垃圾学渣求毕业 2022-01-03 04:41 采纳率: 50%
浏览 143
已结题

c++ Nim取子游戏 博弈论

Problem description:
There are N heaps of coins on the table ordered in a row. In the i-th heap there are exactly ai coins.
Additionally, it is known that ai ≤ aj if i < j.
Alice and Bob play a game with these coins. At each move it is allowed to take any positive amount of coins from any heap, but the condition (ai ≤ aj if i < j) must remain satisfied. The player who takes the last coin, wins.
You should find who wins if no one makes a mistake and Bob gets to make the first move.

Input
The input consists of
• one line containing N (1 ≤ N ≤ 10^5) – the number of heaps
• one line containing N numbers a1, a2, ..., aN (1 ≤ ai ≤ 10^9) – the sizes of the heaps.

Output
If Bob wins output “Bob", otherwise output “Alice".

InputOutput
1Bob
10-
------------
2Alice
10 10-
------------
2Bob
10 15-

另有一组较大的测试数据,600个不同的数字

根据题意我提交的代码如下,虽然测试数据都是对的,但是并没有通过,希望大家可以帮忙看下哪里出了问题呢?

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 100010;

/*
Lose First :a1 ^ a2 ^ a3 ^ ... ^an = 0
Win First:a1 ^ a2 ^ a3 ^ ... ^an ≠ 0
*/

int main()
{

    int n;
    cin >> n;

    int res = 0;
    while (n--)
    {

        int a;
        cin >> a;
        res ^= a;
    }

    if (res) puts("Bob");
    else puts("Alice");

    return 0;
}
  • 写回答

4条回答 默认 最新

  • 仰望星空的代码 领域专家: 前端开发技术领域 2022-01-04 10:36
    关注

    应该是没有问题的。

    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N = 100010;
    /*
    Lose First :a1 ^ a2 ^ a3 ^ ... ^an = 0
    Win First:a1 ^ a2 ^ a3 ^ ... ^an ≠ 0
    */
    int main()
    {
        int n;
        cin >> n;
        int res = 0;
        while (n--)
        {
            int a;
            cin >> a;
            res ^= a;
        }
        if (res) puts("Bob");
        else puts("Alice");
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月9日
  • 赞助了问题酬金 1月4日
  • 修改了问题 1月3日
  • 创建了问题 1月3日