You have $N, price of each chocolate is $C, and you can get one free chocolate for each M wrapper. print maximum chocolate you can eat.
Solution Idea: first count how many chocolates you can buy with $N. it simply N/C.
eat all chocolates, now you have the wrappers. if you have more than M wrappers, return multiple of M wrappers, let, M*X wrappers where X>0 to get X more free chocolates. eat them, now count how many wrapper you have, continue the procedure until you don't have enough wrapper to have a free chocolate.
Example: for N=10, C=2, M=3.
you can buy 5 chocolate with $10, now you have 5 wrapper. return 3 wrapper to get a free chocolate, now you have 2 wrapper and 1 wrapper after eating the free chocolate. so total wrapper=3, return 3 wrapper to get another free chocolate.
so, total chocolate= 5 (buy) + 2 (free) = 7.
Code: 
- #include<bits/stdc++.h>
 - using namespace std;
 - #define SET(a) memset(a,-1,sizeof(a))
 - #define CLR(a) memset(a,0,sizeof(a))
 - #define PI acos(-1.0)
 - int main()
 - {
 - int tc, n, c, m, tch, wr, tmp;
 - cin>>tc;
 - while(tc--)
 - {
 - cin>>n>>c>>m;
 - tch=wr=n/c;
 - while(wr>=m)
 - {
 - tmp=wr/m;
 - wr=wr%m;
 - wr+=tmp;
 - tch+=tmp;
 - }
 - cout<<tch<<endl;
 - }
 - return 0;
 - }
 
Similar Problems:
The Coco-Cola Store
Cola
Soda Surpler

No comments:
Post a Comment