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.

can any1 help me in solving this ques with the help by providing some logic and hint for code snippet.
please explain the the meaning of second paragraph.what do i have 2 do with 1,000,000,000.

Recommended Answers

All 6 Replies

If you exchange a block of m grams, you get three blocks of weight m/2, m/3 and m/4 grams each.

Is the important part of this question because 1/2 + 1/3 + 1/4 = 13/12 or more than you started with. However, fractions are rounded down, so for example a 24g block can be exchanged for a 12g, 8g and 6g block or 26g in total, an increase in value but a 5g block would be exchanged for 2g, 1g and 1g block or 4g in total a reduction in value.

This means that for the 5g block you would be better off exchanging it for money because 5g = 5 units of cash.

The question is to print the largest value of cash you can obtain for a given block size so for any given block size you need to work out if you loose money by splitting it into smalller blocks blocks and if so change it for cash. You probably want to keep doing this in an iterative process until you nolonger have any blocks only cash and at that point see how much cash you have.

The values 2 and 1,000,000,000 denote the minimum and maximum value you have to deal with as input to your program.

no i don't have to put it in iterative process,
for eg
Sample input 2 2
Sample output 2 2
Explanation: If you exchange 2 grams into smaller blocks, it gives 2/2 = 1, 2/3 =
0, 2/4 = 0, only 1 unit. Instead, you can directly exchange the block for 2 units of
currency.
is my code right or wrong??

 void main()
 {
 int m,n1,n2,n3,sum=0;
 if(m>=2&&m<=1000000000)
 {
 n1=m/2;
 n2=m/3;
 n3=m/4;
 sum=n1+n2+n3;
 }
 }

Check this Code Chef link. Its the same program. Many solutions are given.

commented: Giving him the code will not help him learn -2

It is iterative because you can make as many exchanges as you like so for example starting with 1 9000g block you swap for 4500g, 3000g and 2250g blocks but then you can continue and swap the 4500g for 2250g, 1500g and 1125g blocks further increasing the value (2250+1500+1125 = 4875) and I can keep on swaping until I get to block sizes that are not worth swaping because they are too small hence I iteratively swap the blocks until reach an optimal size, remember the aim of the program is to calculate the maximum value I can attain for my original block.

The method you suggest only performs 1 swap so 9000g becomes 4500g, 3000g and 2250g giving a value of 9750 which is greater than the original 9000 but by continue swapping you can get a final value of 14620 which is significantly higher than either the start value or your calculated value.

In your actual code you never initialise m to anything before using it which is actually undefined behaviour.

m is accepted from user...
scanf("%d",&m);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.