Day11

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


emm结论题,如何想到斐波那契数列呢,大概是因为不能超过上次的2倍吧,和斐波那契数列性质相似,拍一个斐波那契博弈学习博客
https://blog.csdn.net/dgq8211/article/details/7602807
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;

const int maxm = 50;

LL fibo[maxm];

void run_case() {
    fibo[1] = fibo[2] = 1;
    for(int i = 3; i < maxm; ++i) fibo[i] = fibo[i-1] + fibo[i-2];
    int n;
    while(cin >> n && n) {
        bool flag = true;
        for(int i = 1; i < maxm; ++i)
            if(n == fibo[i]) {
                flag = false;
                break;
            }
        if(flag) cout << "First win
";
        else cout << "Second win
";
    }
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    //int t; cin >> t;
    //while(t--)
    run_case();
    cout.flush();
    return 0;
}
View Code

原文地址:https://www.cnblogs.com/GRedComeT/p/12312398.html