HDU 2516 取石子游戏( FIB博弈 )

来源:互联网 发布:淘宝上买衣服可靠吗 编辑:程序博客网 时间:2024/05/23 17:24

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516

题       意:1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的

                    石子不能超过上次取子数的2倍。取完者胜.先取者负输出"Second win".先取者胜输出"First win".

思       路:典型的FIB博弈应用(即n的值等于FIB数时,选手必败)。

代码如下:

#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <climits>#include <algorithm>#define maxn  65535typedef __int64 LL;LL a[50];LL inf = 2147483658;int main(){    int i;    a[1]=1;    a[2]=1;    for( i = 3; i < 50; i ++ )    {        a[i] = a[i-1] + a[i-2];        if( a[i] >= inf ) break;    }    int n;    while( scanf ( "%d", &n ) != EOF )    {        if( n == 0 ) break;        int f = 0;        for( int j = 0; j < i; j ++ )        {            if( a[j] == n ) {f=1;break;}            if( a[j] > n ) break;        }        if(f)            printf("Second win\n");        else            printf("First win\n");    }    return 0;}

0 0
原创粉丝点击