题目:共有1000瓶啤酒,每喝完一瓶得到一个空瓶子,每3个空瓶子又能换1瓶啤酒,喝掉以后又可以得到一个空瓶子,问总共能喝到多少瓶啤酒,最后还剩余多少个空瓶子?
用C++简单写了一个:
#pragma region Quiz requirements // The Quiz is to calculate the number of bottles: // There are 1000 bottles of beer, 3 empty bottles can // trade for another additional beer // How many bottles will it be and how many empty // bottles will be left? #pragma endregion #include <iostream> using std::cout; using std::endl; int main() { int total = 1000; int full = 1000; int empty = 0; for(full=1000; full >0 ; full--) // stop until you finish every full beer bottle available { ++ empty; if(empty == 3) { empty = 0; ++ full; ++ total; } } cout << "Total number of bottle is: " << total << endl; cout << "Total empty bottle left is: " << empty << endl; return 0; }
最的的结果如下: