zoukankan      html  css  js  c++  java
  • 1069 Nim游戏

    有N堆石子。A B两个人轮流拿,A先拿。每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N及每堆石子的数量,问最后谁能赢得比赛。
    例如:3堆石子,每堆1颗。A拿1颗,B拿1颗,此时还剩1堆,所以A可以拿到最后1颗石子。
     
    Input
    第1行:一个数N,表示有N堆石子。(1 <= N <= 1000)
    第2 - N + 1行:N堆石子的数量。(1 <= A[i] <= 10^9)
    Output
    如果A获胜输出A,如果B获胜输出B。
    Input示例
    3
    1
    1
    1
    Output示例
    A
     分析:异或和为0则B胜,否则A胜。
    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <map>
    #define range(i,a,b) for(int i=a;i<=b;++i)
    #define LL long long
    #define rerange(i,a,b) for(int i=a;i>=b;--i)
    #define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
    using namespace std;
    int n,ans;
    void init() {
        cin>>n;
        range(i,1,n){
            int tmp;
            cin>>tmp;
            ans^=tmp;
        }
    }
    void solve(){
        cout<<(ans?"A":"B")<<endl;
    }
    int main() {
        init();
        solve();
        return 0;
    }
    View Code
  • 相关阅读:
    Linux
    bzoj 1834
    bzoj 1002 找规律(基尔霍夫矩阵)
    bzoj 1005 组合数学 Purfer Sequence
    bzoj 1601 最小生成树
    bzoj 1001 平面图转对偶图 最短路求图最小割
    bzoj 1192 二进制
    bzoj 1012 基础线段树
    bzoj 1044 贪心二分+DP
    bzoj 1011 近似估计
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9331070.html
Copyright © 2011-2022 走看看