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.

Well, not sure about this formula but if you wanted to use one yourself, think about the steps you would take if you were doing the exchange of wrappers by hand; by the way, not the best code to write as it's confusing and you are writing over the money variable instead of creating a new one. First you buy as many chocolates as you want, chocolates = money/cost . Now you eat those chocolates and exchange all the wrappers possible for extra ones, let's call it extras = chocolates / extra .

BUT you could have leftover chocolates so let's see how many, remainders = chocolates % extra . Since we got extras , we can add it to the remainders and see if it is enough for more chocolates, super_extras = (extras + remainders) / extra . Remember that all of this is integer division so it'll round down.

Finish by adding to the total chocolates += extras + super_extras and you're done! You have to tweak it a bit to get edge cases but that part should be trivial. Use the same logic to see where those terms you wrote came from. I think if you rearrange what I showed you'll get the same thing.

Chk this :Worked for all test cases:
for(int a1=0;a1

no = n/c;
int wrapper = no;
int rem = 0;
while(wrapper>=m){
rem = wrapper/m;
no = no +rem;
rem = rem + wrapper%m;
wrapper = rem;
}
System.out.println(no);
}

Yeah! You are right and the description is easy. I sloved this problem with the same way. The following is my AC code :)

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t,n,c,m;
cin>>t;
while(t--){
cin>>n>>c>>m;
int answer=0;
// Computer answer(the total of chocolates Bob eats)
// answer consists of two parts: directly buy with money and exchange with wrapper
int answer1 = n / c; // the number of buying with money
answer += answer1;
int answer2 = 0; // the number of exchanging with wrapper
int wrapper = answer1 / m; // the number of first exchange with wrapper
int remain = answer1 % m; // the number of first remain wrapper
answer2 += wrapper;
int available = wrapper + remain;
while (available / m != 0) {
wrapper = available / m;
remain = available % m;
answer2 += wrapper;
available = wrapper + remain;
}
answer += answer2;
cout<<answer<<endl;
}
return 0;
}

t = int(raw_input().strip())
for a0 in xrange(t):
n,c,m = raw_input().strip().split(' ')
n,c,m = [int(n),int(c),int(m)]
result = 0
ch = n / c
result +=ch
while True:
ch_w = ch / m
result += ch_w
ch_r = ch % m
ch = ch_w + ch_r
if ch < m:
break
print result

## Chocolate Feast

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

Well, not sure about this formula but if you wanted to use one yourself, think about the steps you would take if you were doing the exchange of wrappers by hand; by the way, not the best code to write as it's confusing and you are writing over the

`money`

variable instead of creating a new one. First you buy as many chocolates as you want,`chocolates = money/cost`

. Now you eat those chocolates and exchange all the wrappers possible for extra ones, let's call it`extras = chocolates / extra`

.BUT you could have leftover

`chocolates`

so let's see how many,`remainders = chocolates % extra`

. Since we got`extras`

, we can add it to the`remainders`

and see if it is enough for more chocolates,`super_extras = (extras + remainders) / extra`

. Remember that all of this is integer division so it'll round down.Finish by adding to the total

`chocolates += extras + super_extras`

and you're done! You have to tweak it a bit to get edge cases but that part should be trivial. Use the same logic to see where those terms you wrote came from. I think if you rearrange what I showed you'll get the same thing.For just one test case input() There needs changing in condition. What if super_extras is more than m. Then more candies.

Chk this :Worked for all test cases: for(int a1=0;a1

Thanks for your comment. You are right, the code is confusing due the name of the variables :) I updated it.

I am not able to figure out the mistake in this code any help wud be appreciated

## include

## include

## include

## include

int main() { long int a,b,c,t,d,e; scanf("%ld",&t); while(t--) { scanf("%ld %ld %ld",&a,&b,&c); e=a/b; d=e;

return 0; }

Yeah! You are right and the description is easy. I sloved this problem with the same way. The following is my AC code :)

Similar approach using python: