Little Bobby loves chocolate. He frequently goes to his favorite store, Penny Auntie, to buy them. They are having a promotion at Penny Auntie. If Bobby saves enough wrappers, he can turn them in for a free chocolate.
Example
He has to spend, bars cost , and he can turn in wrappers to receive another bar. Initially, he buys bars and has wrappers after eating them. He turns in of them, leaving him with , for more bars. After eating those two, he has wrappers, turns in leaving him with wrapper and his new bar. Once he eats that one, he has wrappers and turns them in for another bar. After eating that one, he only has wrapper, and his feast ends. Overall, he has eaten bars.
Example:
Input: n = 10, c = 2, m = 5
Output: 6
Approach
C++
#include <bits/stdc++.h>using namespace std;int chocolateFeast(int n, int c, int m){int choco = n / c, eat = 0, wp = 0;while (choco > 0){eat += choco;wp += choco;choco = wp / m;wp = wp % m;}return eat;}int main(){int n = 10;int c = 2;int m = 5;cout << chocolateFeast(n, c, m) << "\n";return 0;}
No comments:
Post a Comment