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);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Chocolate Feast
You are viewing a single comment's thread. Return to all comments →
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.