UVa 10165 - Stone Game

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

方法參考: Nim

而題目要求的是拿到最後一個贏,所以當所有堆中的石子做 XOR 的結果:

是 0 時,代表我們會輸,反之則是贏,因為我們可以讓它變成 0 (安全殘局)。

Code

UVa 10165
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<cstdio>

int main()
{
int n;
while (scanf("%d", &n) && n)
{
int ans = 0;
while (n--)
{
int temp;
scanf("%d", &temp);
ans ^= temp;
}
puts(ans ? "Yes" : "No");
}

return 0;
}