Water bottleTime limit (Common/Java): 1000 ms/3000 Ms running memory limit: 65536 Kbyte total submission: 238 tested: 156
Description
There is such a smart question: "A store stipulates that three empty water bottles can be changed to a bottle of soda. John has ten empty water bottles on her hand. How many bottles of soda can she change ?" The answer is 5 bottles. The method is as follows: first use 9 empty bottles for 3 bottles of soda, drink 3 full bottles, and then use 3 to replace 4 empty bottles after drinking, the bottle is full, and there are 2 empty bottles at this time. Then, you ask the boss to lend you a bottle of soda first. After drinking the bottle, use three empty bottles for another full bottle and return it to the boss. If John hasNHow many bottles of soda can I use for empty water bottles?
Input
The input file contains up to 10 groups of test data. Each Data occupies one row and only contains one positive integer.N(1 <=N<= 100), indicating the number of empty steam bottles on the hand of Mr. Zhang.N= 0 indicates that the input is over. Your program should not process this line.
Output
For each group of test data, one line is output, indicating the maximum number of steam bottles that can be consumed. If you cannot drink a bottle, the output is 0.
Sample Input
3
10
81
0
Sample output
1
5
40
#include <iostream> using namespace std; int f(int n) { if(n==1)return 0; else if(n==2)return 1; else return n/3+f(n/3+n%3); } int main() { int n; while(cin>>n,n) { cout<<f(n)<<endl; } return 0; }
View code
# Include <iostream>
Using namespace STD;
Int F (int n)
{
If (n = 1) return 0;
Else if (n = 2) return 1;
Else return N/3 + f (N/3 + N % 3 );
}
Int main ()
{
Int N;
While (CIN> N, N)
{
Cout <F (n) <Endl;
}
Return 0;
}
#include <iostream>using namespace std;int main(int argc, char *argv[]){ int n,m,s; while(cin>>n,n) { s=0; while(n) { if(n==2) n=n+1; s +=n/3; if(n==3) n=0; else n=n/3+n%3; } cout<<s<<endl; } return 0;}
View code
# Include <iostream>
Using namespace STD;
Int main (INT argc, char * argv [])
{
Int n, m, S;
While (CIN> N, N)
{
S = 0;
While (N)
{
If (n = 2) n = n + 1; // For example, 6 first executes n = N/3 + N % 3; ------- n = 2 run S + = N/3 ------- S = 0 + 2 = 2
S + = N/3; // re-Execute (n = 2) n + 1 ------ n = 3 simultaneously execute S + = N/3 ------- S = 2 + 3/3 = 3
// Finally execute (n = 3) -------- n = 0 --------------------- to end the loop
If (n = 3) n = 0;
Else n = N/3 + N % 3;
}
Cout <S <Endl;
}
Return 0;
}