题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
#include <iostream> using namespace std; int BottleNum(int Allnum) { int n1 = Allnum / 3; //置换空瓶得到的饮料数 int n1_re = Allnum % 3; //剩下空瓶,不足以置换饮料 int num = n1; int Emptynum = n1 + n1_re; while (Emptynum >= 2){ //新饮料瓶 + 剩下空瓶,是否可以通过借空瓶来换 //2是因为只有两空瓶时,可向老板借1个,喝完还回去,此时手里没瓶子了。 Emptynum = n1 + n1_re; n1 = (Emptynum) / 3; n1_re = (Emptynum) % 3; if (n1 == 0 && n1_re == 2){ num += 1; break; } num += n1; } return num; } int main() { int input; while (cin >> input){ if (input > 0){ cout << BottleNum(input) <<endl; } } return 0; }