ZeroJudge B186: Cookie, Cake, and Chocolate!

There is a store currently running a promotion where customers will receive an additional box of chocolates if they buy 10 cookies and 2 cakes. Please output the final number of items to be given away through the program.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
Each input consists of a single line containing 3 integers, representing the quantities of cookies, chocolates, and cakes, separated by a space.Please output the quantity of items to be given. The question was made for Chinese/Mandarin programmers, so the outputs are in Chinese. You can assume that the following output means 12 cookies, 7 chocolates, and 3 cakes. You can copy and paste the none numeral characters to output.
12 6 312 個餅乾,7 盒巧克力,3 個蛋糕。

Thought Process

You can use a while loop, where the termination condition is if cookies are less than 10 or cakes are less than 2. If either of these conditions occurs, the loop terminates. Within the loop, simply increment the number of chocolates by 1, decrease the number of cookies by 10, and decrease the number of cakes by 2. Note: The original numbers of cookies and cakes need to be stored in two other variables that will not be modified. Finally, just output the original number of cookies and cakes along with the total number of chocolates.

Sample Code-ZeroJudge B186: Cookie, Cake, and Chocolate!

#include <iostream>
using namespace std;

int main() {
    int cookie, cake, chocolate;
    while (cin >> cookie >> chocolate >> cake)
    {
        int nCookie = cookie, nCake = cake;
        while (nCookie >= 10 && nCake >= 2)
        {
            chocolate++;
            nCookie -= 10;
            nCake -= 2;
        }
        cout << cookie << " 個餅乾," << chocolate << " 盒巧克力," << cake << " 個蛋糕。\n";
    }
}

//ZeroJudge B186
//Dr. SeanXD

Comments