HDU 2516 取石子游戏(斐波那契博弈)

取石子游戏

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6613    Accepted Submission(s): 3994


Problem Description

1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍。取完者胜.先取者负输出"Second win".先取者胜输出"First win".
 

Input

输入有多组.每组第1行是2<=n<2^31. n=0退出.
 

 

Output

先取者负输出"Second win". 先取者胜输出"First win". 
参看Sample Output.
 

 

Sample Input

2 13 10000 0
 

Sample Output

Second win Second win First win

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 int f[50];
 8 
 9 int main () {
10     f[1] = 1;f[2] = 2;
11     for (int i=3; i<=45; ++i) {
12         f[i] = f[i-1] + f[i-2];
13     }
14     int n;
15     while (cin >> n && n) {
16         bool flag = false;
17         for (int i=1; i<=45; ++i)
18             if (f[i] == n) {flag = true;break;}
19         if (flag) puts("Second win");
20         else puts("First win");
21     }
22 }
原文地址:https://www.cnblogs.com/mjtcn/p/8477844.html