20-取石子动态规则(hdu2516 斐波那契博弈)

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): 6603    Accepted Submission(s): 3987

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
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2509 2512 1536 2510 1907 
 
思路:此题是动态的规则,对于先手,如果n是斐波那契数则为败局。
#include <iostream>
using namespace std; int main(){
int n, m;
int a[50] = {2, 3};
for(int i = 2; i < 50; i++){
a[i] = a[i - 1] + a[i - 2];
}
while(cin >> n && n){
int flag = 0;
for(int i = 0; i < 50; i++){
if(a[i] == n){
flag = 1;
break;
}
}
if(flag){
cout << "Second win" << endl;
}
else{
cout << "First win" << endl;
}
}
return 0;
}

  

上一篇:Redis高可用集群


下一篇:微软职位内部推荐-Senior Software Development Engineer_Commerce