题目:有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
自己的笨解法:
1 #include<iostream> 2 #include<math.h> 3 #include<string.h> 4 5 using namespace std; 6 7 int get_result(int n) 8 { 9 int result = 0; 10 if(n < 2) 11 result = 0; 12 else{ 13 while(floor(n / 3) > 0) 14 { 15 result = result + floor ( n / 3 ); 16 n = floor ( n / 3 ) + ( n % 3); 17 } 18 } 19 if ( n % 3 == 2) 20 result ++ ; 21 return result; 22 } 23 24 int main(){ 25 int result = 0; 26 int i = 0; 27 int n = 0; 28 while (cin >> n) 29 { 30 if( n <= 0) 31 break; 32 else{ 33 result = get_result(n); 34 cout << result << endl; 35 } 36 //int result = 0 ; 37 //result = get_result(n); 38 //cout << result << endl; 39 } 40 41 // cout << i << endl; 42 return 0; 43 }
大神解法:(来源:牛客网)
1 #include<stdio.h> 2 3 int main (){ 4 5 int m; 6 7 while(~scanf("%d",&m)&&m!=0) printf("%d\n",m/2); 8 9 return 0; 10 11 }