ZeroJudge N803: The Coco-Cola Store

UVa 11877 – The Coco-Cola Store

Once upon a time, there was a special Coca-Cola store. If you return three empty bottles to the store, you will receive one full bottle of Coca-Cola to drink. If you have N empty bottles, how many bottles of Coca-Cola can you drink at most?

Hint: Let me tell you how to drink 5 bottles of Coca-Cola with 10 empty bottles: Use 9 empty bottles to exchange for 3 full bottles, drink them to get 3 empty bottles, then use these empty bottles to exchange for one full bottle. Now you have 2 empty bottles. Borrow one empty bottle from the store, then exchange for one full bottle. Drink it, and finally return the last empty bottle to the store.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
EOF input. Each test case consists of one line containing an integer N (1 ≤ N ≤ 100). The input ends with N = 0, which should not be processed.For each test case, output the number of full bottles of Coca-Cola you can drink.
3
10
81
0
1
5
40

Thought Process

Use a while loop (N >= 3). Inside the loop, declare a variable 'bottle' representing the number of Coca-Cola bottles you can drink, which is N/3. Then, update the answer += bottle, N -= bottle*3, and N += bottle.

Since you can borrow empty bottles from the store, add a conditional statement outside the while loop. If N == 2 after the while loop ends, it means you can borrow a bottle, so increment the answer by 1.

Sample Code-ZeroJudge N803: The Coco-Cola Store

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    while (cin >> N && N != 0) {
        int ans = 0;
        while (N >= 3) {
            int bottle = N/3;
            ans += bottle;
            N -= bottle*3;
            N += bottle;
        }
        if (N == 2) ans++;
        cout << ans << "\n";
    }
}

//ZeroJudge N803
//Dr. SeanXD

Comments