We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

First, your total (initial) number of chocolates, and wrappers, is equal to what you can buy with cash, which is n/c. Now you have to iteratively give your wrappers to the shop keeper, in exchange for chocolates, costing you m wrappers (wrappers / m). During this iterative process you add the chocolates to your total count. You subtract the number of wrappers you have handed in and also add back the number of wrappers you receive from the chocolates you are getting back. Here's a java solution.

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
int n = in.nextInt();
int c = in.nextInt();
int m = in.nextInt();
int total = n/c;
int wrappers = total;
while(wrappers >= m) {
total += (wrappers / m);
int leftover = wrappers % m;
wrappers = (wrappers / m) + leftover;
}
System.out.println(total);
}
}

## Chocolate Feast

You are viewing a single comment's thread. Return to all comments →

Following code works for me:

wrappers = n/c;

count = wrappers;

while (wrappers >= m) {

wrappers = wrappers - m;

count = count + 1;

wrappers = wrappers + 1;

}

System.out.println (count);

I couldn't figure out the algo. Pls help.

First, your total (initial) number of chocolates, and wrappers, is equal to what you can buy with cash, which is n/c. Now you have to iteratively give your wrappers to the shop keeper, in exchange for chocolates, costing you m wrappers (wrappers / m). During this iterative process you add the chocolates to your total count. You subtract the number of wrappers you have handed in and also add back the number of wrappers you receive from the chocolates you are getting back. Here's a java solution.

That's exactly what I did.

That's basically what I did, except in Python.

A better version:

Similar here without iterating every bar/wrapper swap

Same as mine!

i dont get it, why sum plus one on wrappers at the end of the loop... ? that was my mistake...

every time you subtract m wrappers you get one chocolate so one more wrapper so you add 1

You are just right

brother