Problem Description
1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍。取完者胜.先取者负输出"Secondwin".先取者胜输出"Firstwin".
Input
输入有多组.每组第1行是2<=n<2^31.n=0退出.
Output
先取者负输出"Secondwin". 先取者胜输出"Firstwin".
参看Sample Output.
Sample Input
2
13
10000
0
Sample Output
Second win
Second win
First win
思路:
#include<iostream> using namespace std; int main() { //计算到f[48]已经接近超出int的数据范围了 int f[48]; f[0] = 0, f[1] = 1; int i, n; for (i = 2; i < 48; i++) f[i] = f[i-1] + f[i-2]; while(cin>>n && n!=0) { bool flags = 0; //根据题目意思,n>=2的,所以从f[3] = 2开始 for (i = 3; i < 48; i++) { if (n == f[i]) { flags = 1; break; } } if(flags) cout<<"Second win"<<endl; else cout<<"First win"<<endl; } return 0; }
原文地址:http://blog.csdn.net/wtyvhreal/article/details/42524697