取石子游戏
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2524 Accepted Submission(s): 1443
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
Sample Output
Second win Second win First win
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20……
p p n p n n p n n n n p n n n n n n n……
以上PN图可以很容易画出来,必败状态为2,3,5,8,13.。。。其实我没画21时的状态,因为看到必败状态序列是菲波那切数列,所以就直接猜想必败状态符合斐波那契,提交后竟然AC了。。。。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <climits>
using namespace std;
int a,b;
int main(){
//freopen("in.txt","r",stdin);
//(author : CSDN iaccepted)
int n,ts;
bool mark;
while(scanf("%d",&n) && n){
a = 2,b = 3;
mark = false;
while(a<=n){
if(a==n || b==n){
mark = true;
break;
}
ts = (a + b);
a = b;
b = ts;
}
if(mark){
printf("Second win\n");
}else{
printf("First win\n");
}
}
return 0;
}