斐波纳契博弈
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 0Sample Output
Second win Second win First win
题意 : 两个人轮流取石子,先取完的获胜
思路分析 : 想了好久没推出来,最后还是大神告的,是个斐波纳契博弈
代码示例 :
#define ll long long ll pre[100]; int main() { ll n; int pos; pre[1] = 2, pre[2] = 3; for(int i = 3; i <= 100; i++){ pre[i] = pre[i-1] + pre[i-2]; pos = i; if (pre[i] > 10000000000) break; } while(scanf("%lld", &n) && n){ int sign = 0; for(int i = 1; i < pos; i++){ if (n == pre[i]) {sign = 1; break;} } if (sign) printf("Second win\n"); else printf("First win\n"); } return 0; }
东北日出西边雨 道是无情却有情