题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
#include <iostream> using namespace std; int bottle( int& n) { int cur_empty_bottle = n;//当前的空瓶数 int cur_water_botter = 0;//当前的空瓶可以兑换的汽水 int res = 0;//记录结果 while( true ) { cur_water_botter = cur_empty_bottle / 3 ; res += cur_water_botter; cur_empty_bottle = cur_water_botter + cur_empty_bottle % 3; if( cur_empty_bottle == 1 ) break;//最终空瓶为1,结束 if( cur_empty_bottle == 2 ) {//最终空瓶为2,可以借一个,结果加1 res ++; break; } //空瓶为1,结果为0; //空瓶为2,结果为1(可以借一个嘛) //空瓶为3,换一瓶汽水,循环一次,空瓶又变成了1 //空瓶为4,结果同2 //所以while循环只考虑1和2的情况 } cout<< res << "\n"; return 0; } int main() { int n = 0; while( cin >> n ) { if( n == 0 ) break; bottle(n); } return 0; } // 64 位输出请用 printf("%lld")