1069 Nim游戏

1069 Nim游戏

基准时间限制:秒 空间限制:131072 KB 

N堆石子。A B两个人轮流拿,A先拿。每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N及每堆石子的数量,问最后谁能赢得比赛。

例如:3堆石子,每堆1颗。A1颗,B1颗,此时还剩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

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
         while(sc.hasNext()){
         int N=sc.nextInt(),i,ans = 0,m;
         for(i=0;i<N;i++){
             m=sc.nextInt();
             if(i==0) ans=m;
             else
                 ans=ans^m;
         }
         if(ans>0)
             System.out.println("A");
         else
             System.out.println("B");
        
         }
         sc.close();
    }

}
原文地址:https://www.cnblogs.com/watchfree/p/5350734.html