Matches Game

Here is a simple game. In this game, there are several piles of matches and two players. The two player play in turn. In each turn, one can choose a pile and take away arbitrary number of matches from the pile (Of course the number of matches, which is taken away, cannot be zero and cannot be larger than the number of matches in the chosen pile). If after a player’s turn, there is no match left, the player is the winner. Suppose that the two players are all very clear. Your job is to tell whether the player who plays first can win the game or not.

Input

The input consists of several lines, and in each line there is a test case. At the beginning of a line, there is an integer M (1 <= M <=20), which is the number of piles. Then comes M positive integers, which are not larger than 10000000. These M integers represent the number of matches in each pile.

Output

For each test case, output "Yes" in a single line, if the player who play first will win, otherwise output "No".

Sample Input

2 45 45
3 3 6 9

Sample Output

No
Yes

好吧,这个博弈题,让我来说一下,你每次输入一个n代表n堆数字,然后每堆数字都代表这一堆有多少个棋子,你每次至少取一个棋子,但是只能选择一堆。好,那我们来讨论把。

当n==1时,先手必胜(全部取光)

当n==2时,需要判断两个堆的数字情况,例如每堆都是1,那么后手赢了(一人一个)

当n==2时,但是一堆是1另外一对是x,则先手赢(先手需要在x那堆取x-1个就赢了)

当n==2时,但是两堆没有一个是1,但是两堆的数量相同,那么先手输了(因为无论先手你做什么操作,后手只需要模仿你,在另外一堆上面进行一样的操作,你就输了)

当n==2时,两堆不相等并且均不等于1时,那么先手必胜(你想一下啊,你是不是可以把两堆取成一样的,然后你就进入到你掌控世界的节奏当中了

当n==3时,三堆都是1,不用说肯定先手赢了。

当n==3时,两堆是1,另外一堆是x,那么先手赢了(把x那堆取光,然后进入1,1的那种情况了)

当n==3时,一堆是1,另外两堆相等并且不等于1的时候,先手赢了(先手把1的那堆取光,就变成了上面的两堆相等的情况)

当n==3时,一堆是1,另外两堆不相等并且不等于1的时候后手赢了(先手把一堆取光,那么剩下两堆就变得相等了,要是先把两堆变得相等,那么自己就会成为被模仿的对象)

当n==3时,三堆均不相等,并且都不等于1的时候,先手赢(先手可以把任意一堆变成1,然后变成所谓的“后手”形成上面说的那种情况)

当然还有很多,我现在就不列举了,开始找规律,我先初始化一个标记等于0,如果标记等于1就是先手赢,然后标记是0,就是后手赢,我需要让标记通过某种操作和正确结果对应。

当n==1时,看不出来,当n==2时,你发现结果总是围绕着1展开,于是二进制出现,你会发现当两堆均相等时候,先手输了,剩下的都是先手赢,然后当n==3时,你会发现只要被后手形成只有两堆并且相等那么先手就输了,所以他其实在判断相等的数字个数。

好了我们发现二进制这个异或的好东西,可以直接判断两个数字是否相等,相等是0值不变,不相等是1。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int flag=0;
        long long x;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&x);
            flag^=x;
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that! Here's an example implementation of the GuessingGame in Java: ```java // GuessingGame.java import java.util.Scanner; public class GuessingGame { private String secretWord; private int guessesLeft; public GuessingGame(String secretWord, int maxGuesses) { this.secretWord = secretWord; this.guessesLeft = maxGuesses; } public void play() { String guessedWord = ""; for (int i = 0; i < secretWord.length(); i++) { guessedWord += "*"; } Scanner scanner = new Scanner(System.in); while (guessesLeft > 0) { System.out.println("Guess a letter (" + guessesLeft + " guesses left):"); String guess = scanner.nextLine(); // Check if guess is correct boolean correctGuess = false; for (int i = 0; i < secretWord.length(); i++) { if (secretWord.charAt(i) == guess.charAt(0)) { guessedWord = guessedWord.substring(0, i) + guess + guessedWord.substring(i + 1); correctGuess = true; } } if (!correctGuess) { guessesLeft--; } System.out.println("Guessed word: " + guessedWord); if (guessedWord.equals(secretWord)) { System.out.println("Congratulations, you guessed the word!"); return; } } System.out.println("Sorry, you ran out of guesses. The word was " + secretWord); } } ``` And here's an example usage in a main class: ```java // Main.java public class Main { public static void main(String[] args) { GuessingGame game = new GuessingGame("house", 10); game.play(); } } ``` In this implementation, the `GuessingGame` class takes two parameters: the secret word to guess, and the maximum number of guesses allowed. The `play` method runs the game loop, prompting the user for guesses and updating the guessed word accordingly. If the guessed word matches the secret word, the game is won; if the user runs out of guesses, the game is lost.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值