取石子游戏
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2101 Accepted Submission(s): 1205
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
Source
ECJTU 2008 Autumn Contest
Recommend
lcy
//华为2014校招机试题第三题,160分 #include<iostream> #include<cstdio> using namespace std; int main() { //这里为什么声明数列长度为48呢?因为计算到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]; //可以输出看看 //cout<<f[i]<<' '; } //cout<<endl; while(scanf("%d",&n) == 1) { if(n == 0) break; 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; }