分析:利用数论得出的公式:
- 当先开始拿的人,面对的结果异或不为0,那么他就可以把结果异或为零,最终他会赢
- 反之,先开始拿的人面对的是时异或为0,那么他的对手总可以把结果异或为0,最终他的对手会赢。
import java.util.Scanner;
public class Main {
public static int n;
public static void main(String[] args) {
Scanner sca = new Scanner( System.in);
n = sca.nextInt();
int a[] = new int[n];
int result = 0;
for(int i = 0; i < n; i ++) {
a[i] = sca.nextInt();
result ^= a[i];
}
if(result == 0) {
System.out.println("Bob赢");
}
else {
System.out.println("Alice赢");
}
}
}
运行结果:
3
3
4
5
Alice赢