HDU 2516 (博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516
取石子游戏
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2994 Accepted Submission(s): 1748
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
分析:模拟一些数据就能找到规律了,通过模拟可以知道第二个人赢是一个斐波拉契数列。
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 int f[50]; 9 10 int main () 11 { 12 int n; 13 int i,j,ok; 14 while (scanf ("%d",&n)==1&&n) 15 { 16 f[1] = 2; 17 f[2] = 3; 18 for (i=3; i<45; i++) 19 f[i] = f[i-1] + f[i-2]; 20 ok = 0; 21 for (j=1; j<45; j++) 22 { 23 if (n == f[j]) 24 { 25 ok = 1; 26 break; 27 } 28 } 29 if (ok) 30 printf ("Second win\n"); 31 else 32 printf ("First win\n"); 33 } 34 return 0; 35 }