hdu 2516 取石子游戏 (斐波那契博弈)

本文详细解析了HDU2516取石子游戏问题,通过使用斐波那契数列判断游戏胜负策略。利用C++实现代码,展示了如何通过binary_search函数快速判断输入数值是否为斐波那契数,从而决定先手玩家的胜负。

hdu 2516 取石子游戏

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

题解: 根据题意可以判断出这是一到斐波那契的模板题。

#include<list>
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
const int Base =131;
const ll INF = 1ll << 62;
//const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9+7;
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }


ll a[50];
void init()//计算斐波那契数列
{
	a[0]=1;
	a[1]=1;
	for(int i=2;i<=50;i++)
		a[i]=a[i-1]+a[i-2];
}

int main()
{
	ll n;
	init();
	while(~scanf("%lld",&n),n)
	{
		if(binary_search(a,a+50,n))//判断n是不是斐波那契数
			printf("Second win\n");
		else printf("First win\n");
	}
    system("pause");
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值