Title Link: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=23309
Main topic:
With 3 empty Coke bottles can be exchanged for a full bottle of Coke, with an n (1<=n<=100) empty Coke bottle, the most can drink to how many bottles of coke
It is important to note that if you have 10 empty bottles, first 3 bottles of Coke, after drinking 4 empty bottles, and then in exchange for 1 bottles, after drinking after 2 empty bottles, you can borrow an empty bottle, with the 3 empty bottles at this time for a bottle of Coke, drink the return bottle, so that 10 empty bottles can drink to 5 bottles of Coke.
Example:
Sample Input 3 Bayi 0 Sample Output 1 5 40 topic Analysis: According to the tip left 2 empty bottles can continue In exchange for a bottle of Coke, you can use conditional discriminant statements (WHILE,IF, etc.), the number of empty bottles more than 1 can continue to redeem, at this time, there are two kinds of situations to discuss, whether the number of bottles is equal to 2, if, in exchange for a bottle of Coke after the end of the condition of discrimination, otherwise continue. Source:
1#include <iostream>2 using namespacestd;3 intMain ()4 {5 intN,k,count;6 while(cin>>n&&N)7{count=0;8k=0;9 while(n>1)Ten{if(n==2)//The bottle has 2 to borrow 1 drink 1 also 1 One{ ++count; A Break; - } -k=n/3; theCount+=k;//calculate the number of bottles that can be exchanged -n=n-2*k;//calculate the number of empty bottles left after redemption - } -cout<<count<<Endl; + } - return 0; +}
The Coco-cola Store