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

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;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章