取石子游戏
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 Output.
Sample Input
2
13
10000
0
2
13
10000
0
Sample Output
Second win
Second win
First win
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 "); 42 else 43 printf("First win "); 44 } 45 return 0; 46 }
C/C++:
1 #include <map> 2 #include <set> 3 #include <queue> 4 #include <cmath> 5 #include <vector> 6 #include <string> 7 #include <cstdio> 8 #include <cstring> 9 #include <climits> 10 #include <iostream> 11 #include <algorithm> 12 #define INF 0x3f3f3f3f 13 #define LL long long 14 using namespace std; 15 16 __int64 n, num[100] = {1, 1}; 17 18 set <__int64> s; 19 20 void calc() 21 { 22 s.insert(1); 23 for (int i = 2; i <= 45; ++ i) 24 { 25 num[i] = num[i - 1] + num[i - 2]; 26 s.insert(num[i]); 27 } 28 } 29 30 int main() 31 { 32 calc(); 33 while (scanf("%I64d", &n), n) 34 { 35 if (s.find(n) != s.end()) 36 printf("Second win "); 37 else 38 printf("First win "); 39 } 40 return 0; 41 }