取石子游戏----------------------------思维(斐波那契博弈模板)

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
ECJTU 2008 Autumn Contest

Recommend
lcy | We have carefully selected several similar problems for you: 2509 2512 1536 2510 1907

解析:
斐波那契博弈:当n为斐波那契数时,先手必败,否则先手必胜

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll f[55];
int n;
int main()
{
	f[0]=f[1]=1;
	for(int i=2;i<55;i++) f[i]=f[i-1]+f[i-2];
	while(~scanf("%d",&n)&&n)
	{
		int flag=0;
		for(int i=0;i<55;i++)
		{
			if(f[i]==n)
			{
				cout<<"Second win"<<endl;
				flag=1;
				break;
			}
			if(f[i]>n) break;
		}
		if(!flag) cout<<"First win"<<endl;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章