java 通过对一个数组的三个不同位置的不断提问来还原数组

Chiya invite you to play a game. She comes up with an array with length ( is a multiple of 3) whose values are either 0 or 1. But she doesn’t tell you the content of the array, and requests you to guess the array by asking several questions. In one query (i.e. asking), you can choose three distinct positions a,b,c, and she will tell you the most common value of the three positions. That is, if there are more 1s than 0s in xa,xb,xc, she will tell you “1”, and “0” otherwise.One more thing, it’s guaranteed that (1/3)n

public static int query(int a, int b, int c); 

It accepts three arguments a,b,c, indicating the three indices of the array. If the number of 1 s is more than that of 0 s at xa,xb,xc, it will return 1 , and 0 otherwise. By making queries, your task is to recover an array such that x’=x, and return it. That’s all of the core part of the problem.

To reduce unnecessary coding, we provide a template for you to get started and to debug. The class Judge is to help you debug locally, you can execute Judge.main in IDEA after you finish coding. When the program runs, you will first need to enter an integer (the length) and then an array , so that it can act like Chiya to interactive with your code in Main , check whether your code is able to recover the array, and provide some useful debug information.
This class has nothing to do with the final judging on OJ, so you can modify it if you understand, if not, just leave it alone. When submitting your code, you only need to submit your class Main and some necessary import s, do NOT paste the class Judge . That is, submit the code until the line “ DO NOT SUBMIT THE FOLLOWING CODE ”.

public class Main { 
public static int[] solve(int n) { 
// TODO: write your code here 
} 
} 
// !!!! DO NOT SUBMIT THE FOLLOWING CODE !!!! 
class Judge { 
private static int n; 
private static int[] x; 
private static int count = 0; 
public static int query(int a, int b, int c) { 
// check duplicated indices
if (a == b || b == c || a == c) { 
System.err.printf("[!] Duplicated indices: %d %d %d\n", a, b, c); 
System.exit(1); 
} 
// check out-of-bound 
if (a >= n || b >= n || c >= n || a < 0 || b < 0 || c < 0) { 
System.err.printf("[!] Indices out of range: %d %d %d\n", a, b, c); 
System.exit(1); 
} 
count++; // increase the guessing times 
int sum = x[a] + x[b] + x[c]; // count the number of 1s 
System.out.printf("[+] Query %d %d %d => %d\n", a, b, c, sum / 2); 
return sum / 2; // if there are two or more 1s, it will return 1 
} 
public static void main(String[] args) { 
java.util.Scanner scanner = new java.util.Scanner(System.in); 
System.out.print("[*] n: "); 
n = scanner.nextInt(); 
// read an array x[] 
System.out.print("[*] x[]: "); 
x = new int[n]; 
for (int i = 0; i < n; i++) 
x[i] = scanner.nextInt(); 
// call your code 
int[] y = Main.solve(n); 
// compare your answer with the correct array 
boolean correct = true; 
for (int i = 0; i < n; i++) 
if (x[i] != y[i]) 
correct = false; 
System.out.println(); 
System.out.println(correct ? "[+] Correct" : "[-] Wrong"); 
System.out.println("[+] Number of guesses: " + count); 
System.out.println("[+] Your answer: " + java.util.Arrays.toString(y)); 
System.out.println("[+] The array: " + java.util.Arrays.toString(x)); 
} 
}
```java


```