Bash游戏 V2(51Nod-1067)
生活随笔
收集整理的這篇文章主要介紹了
Bash游戏 V2(51Nod-1067)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
有一堆石子共有N個。A B兩個人輪流拿,A先拿。每次只能拿1,3,4顆,拿到最后1顆石子的人獲勝。假設A B都非常聰明,拿石子的過程中不會出現失誤。給出N,問最后誰能贏得比賽。
例如N = 2。A只能拿1顆,所以B可以拿到最后1顆石子。
輸入
第1行:一個數T,表示后面用作輸入測試的數的數量。(1 <= T <= 10000)
第2 - T + 1行:每行1個數N。(1 <= N <= 10^9)
輸出
共T行,如果A獲勝輸出A,如果B獲勝輸出B。
輸入樣例
3
2
3
4
輸出樣例
B
A
A
思路:博弈論
打出 SG?函數的表然后可發現,當 n % 7 == 0 || (n - 2) % 7 == 0 是先手必敗否則先手必勝
源程序
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define E 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD=7; const int N=100000+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std;int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);if((n-2)%7==0||n%7==0)printf("B\n");elseprintf("A\n");}return 0; }?
總結
以上是生活随笔為你收集整理的Bash游戏 V2(51Nod-1067)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重排列(51Nod-2513)
- 下一篇: Firing(POJ-2987)