高僧斗法 类似算法尼姆解法

 1 public class 尼姆_nim {
 2 
 3     public static void main(String[] args) {
 4         Scanner sc=new Scanner(System.in);
 5         int caseNum=sc.nextInt();//输入有几组
 6         int[][] data=new int[caseNum][];
 7         for(int i=0;i<caseNum;i++) {
 8             int k=sc.nextInt();//每组又有多少数据
 9             data[i]=new int[k];
10             for(int j=0;j<k;j++) {
11                 data[i][j]=sc.nextInt();//输入数据
12             }
13         }
14         sc.close();
15         for(int i=0;i<caseNum;i++) {
16             String res=deal(data[i]);//对每组的数据进行处理
17             System.out.println(res);
18         }
19     
20 
21     }
22 
23     private static String deal(int[] a) {
24         int len=a.length;
25         Arrays.sort(a);//将数据进行排序
26         int res=0;
27         if((len&1)==1) {//数据长度是奇数
28             for(int i=0;i<len;i+=2)
29                 res^=(i==0)?(a[0]-1):(a[i]-a[i-1]-1);
30         }else {
31             for(int i=1;i<len;i+=2)
32                 res^=(a[i]-a[i-1]-1);
33         }
34         if(res==0) {
35             return "先手输";
36         }else
37             return "先手胜";
38     }
39 
40 }

猜你喜欢

转载自www.cnblogs.com/lang-zi/p/12411354.html