hdu 2509 Be the Winner(博弈)

题目链接 :hdu 2509 Be the Winner

题目大意:给定n堆苹果的个数,两人轮流操作,每次操作从一堆苹果中取若干苹果,取到最后一个苹果的人位输

解题思路:反Nim游戏。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 50;

int main () {
    int ret = 0, n, x;
    while (scanf("%d", &n) == 1) {
        bool flag = false;
        for (int i = 0; i < n; i++) {
            scanf("%d", &x);
            ret ^= x;
            if (x > 1)
                flag = true;
        }

        if (flag) 
            printf("%s\n", ret ? "Yes" : "No");
        else
            printf("%s\n", n&1 ? "No" : "Yes");
    }
    return 0;
}

你可能感兴趣的:(hdu 2509 Be the Winner(博弈))