简体   繁体   中英

How can I make formula for solve this problem in C++?

charlie once visited chocolate factory to buy some chocolates.Chocolate cost Rs. 3 each and he had only Rs. 45.He was disappointed. He wanted more. But when he reached the factory he was amazed. There was a scheme on that special day. If you return 3 rappers of the chocolate you will get one chocolate for free. So how many chocolates can he buy? For example: Charlie has Rs. 45 and each chocolate cost Rs. 3. So he buys only 15 chocolates. But as per offer He will return 15 wrappers and get 5 chocolates for free. Then he will return 3 wrappers out of 5 and get another one free. And he will again use one wrapper with the remaining 2 wrappers to get one more. So in total he had 15+5+1+1 = 22 chocolates.

You should probably use recursion with this. Until you reach the base case which in this case will be (total/3 < 1) which means that you cannot return any wrapper to get a new one. If it doesnt hit the base case you can do something like

ans=total/3+func(total/3)

This adds the current number of chocolates ie total/3 with the number of chocolates he will get after returning the wrappers of the current number of chocolates. Hope this helps a little :)

jbtw total is basically the total number of wrappers he has at that particular time. OR the total money he has at that particular iteration. and func is the recursive function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM