Vintion's blog

~夜黑路滑,社会复杂~

Chocolate Feast

| Comments

Chocolate Feast

Little Bob loves chocolates, and goes to the store with N money in his pocket. The price of each chocolate is C . The store offers a discount: for every M wrappers he gives the store, he’ll get one chocolate for free. How many chocolates does Bob get to eat?

Input Format:

The first line contains the number of test cases $T (<=1000)$. T lines follow, each of which contains three integers N, C and M

Output Format:

Print the total number of chocolates Bob eats.

Constraints:

1
2
3
$2 \le N \le 10^5$ 
$1 \le C \le N$ 
$2 \le M \le N$

Sample input

1
2
3
4
3
10 2 5
12 4 4
6 2 2

Sample Output

1
2
3
6
3
5

Explanation

In the first case, he can buy 5 chocolates with $\$10$ and exchange the 5 wrappers to get one more chocolate thus making the total number of chocolates he can eat as 6

In the second case, he can buy 3 chocolates for $\$12$. However, it takes 4 wrappers to get one more chocolate. He can’t avail the offer and hence the total number of chocolates remains 3.

In the third case, he can buy 3 chocolates for $\$6$. Now he can give 2 of this 3 wrappers and get 1 chocolate. Again, he can use his 1 unused wrapper and 1 wrapper of new chocolate to get one more chocolate. Total is 5.

The Code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int t,n,c,m,tmp;
    cin>>t;
    while(t--){
        cin>>n>>c>>m;
        int answer  = 0;
        int wrapper = 0;
        while(wrapper>=m||n>=c)
        {
            if(n>=c)
            {
                answer += n/c;
                wrapper += answer;
                n %= c; 
            }
            if(wrapper>=m)
            {
                answer += wrapper/m;
                tmp     = wrapper/m;
                wrapper %= m;
                wrapper += tmp;
            }
        }
        cout<<answer<<endl;
    }
    return 0;
}

Comments