首页 > 其他 > 详细

hdu 2516 取石子游戏 (Fibonacci博弈)

时间:2018-08-30 10:15:34      阅读:116      评论:0      收藏:0      [点我收藏+]

取石子游戏

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


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

 

C/C++:

 1 #include <map>
 2 #include <queue>
 3 #include <cmath>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <climits>
 9 #include <iostream>
10 #include <algorithm>
11 #define INF 0x3f3f3f3f
12 #define LL long long
13 #define wzf ((1 + sqrt(5.0)) / 2.0)
14 using namespace std;
15 
16 __int64 n, fib[45] = {1, 1};
17 
18 void calc()
19 {
20     for (__int64 i = 2; i <= 46; ++ i)
21         fib[i] = fib[i - 1] + fib[i - 2];
22 }
23 
24 bool is_fib()
25 {
26     for (__int64 i = 2; i <= 45; ++ i)
27     {
28         if (fib[i] > n) return false;
29         if (n == fib[i])
30             return true;
31     }
32     return false;
33 }
34 
35 int main()
36 {
37     calc();
38     while (scanf("%I64d", &n), n)
39     {
40         if (is_fib())
41             printf("Second win\n");
42         else
43             printf("First win\n");
44     }
45     return 0;
46 }

 

hdu 2516 取石子游戏 (Fibonacci博弈)

原文:https://www.cnblogs.com/GetcharZp/p/9557816.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!