hdu2516(巴什博弈)

取石子游戏

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2994    Accepted Submission(s): 1748

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
 
分析:模拟一些数据就能找到规律了,通过模拟可以知道第二个人赢是一个斐波拉契数列。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

int f[50];

int main ()
{
    int n;
    int i,j,ok;
    while (scanf ("%d",&n)==1&&n)
    {
        f[1] = 2;
        f[2] = 3;
        for (i=3; i<45; i++)
        f[i] = f[i-1] + f[i-2];
        ok = 0;
        for (j=1; j<45; j++)
        {
            if (n == f[j])
            {
                ok = 1;
                break;
            }
        }
        if (ok)
        printf ("Second win\n");
        else
        printf ("First win\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值