Monday, 28 October 2013

HackerRank October Challenge 2013 Editorial [Chocolate Feast]

Problem: Chocolate Feast
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: 
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define SET(a) memset(a,-1,sizeof(a))
  5. #define CLR(a) memset(a,0,sizeof(a))
  6. #define PI acos(-1.0)
  7.  
  8. int main()
  9. {
  10. int tc, n, c, m, tch, wr, tmp;
  11. cin>>tc;
  12. while(tc--)
  13. {
  14. cin>>n>>c>>m;
  15. tch=wr=n/c;
  16. while(wr>=m)
  17. {
  18. tmp=wr/m;
  19. wr=wr%m;
  20. wr+=tmp;
  21. tch+=tmp;
  22. }
  23. cout<<tch<<endl;
  24. }
  25. return 0;
  26. }

Similar Problems:
The Coco-Cola Store
Cola
Soda Surpler

No comments:

Post a Comment