You have a block of platinum that can be exchanged in your bank either for cash
or for smaller blocks of platinum. If you exchange a block of m grams, you get
three blocks of weight m/2, m/3 and m/4 grams each. You don't get any fractional
part, as the division process rounds down the value. If you exchange the block of
platinum for cash, you get m units of currency. You can do any number of
exchanges for smaller blocks or currency.Given the value of a block in grams as input, write a program that would print the largest possible currency value that you can receive as the output. Assume that
the maximum value of a block that can be given as an input is 1,000,000,000
grams and the minimum value is 2 grams.
1
Expert's answer
2014-12-17T01:51:27-0500
Program #include <iostream> #define MIN_SIZE 2 using namespace std; //cost of pieces size m //calc recursive double cost(int m) { //bottom of recursion - minimal size less MIN_SIZE //i.e. m~m/2+m/3+m/4; m/4<2 or m<8 if(m<8) return m; else { //check: is round of dividing less pieces? if((m/2+m/3+m/4)<m) return m; else return cost(m/2)+cost(m/3)+cost(m/4); } } int main( ) { int m; cout<<"Input weight block ofplatinum (g): "; cin>>m; cout<<"\nYou can receive currencyvalue: "<<cost(m)<<endl; return 0; } Example of execute program: Input weight block of platinum (g): 24 You can receive currency value: 27 Input weight block of platinum (g): 10 You can receive currency value: 10
Comments
Leave a comment