Spy Detected Codeforces-1512A
2021.05.01 训练题F
题目大意: 给出一组数,找出与众不同的那个数的位置
思路: 用三个数据记录,总有两个一样的一个不同的,然后找到不同的那个mark标记位置就可
题目:
You are given an array a consisting of n (n≥3) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array [4,11,4,4] all numbers except one are equal to 4).
Print the index of the element that does not equal others. The numbers in the array are numbered from one.
Input
The first line contains a single integer t (1≤t≤100). Then t test cases follow.
The first line of each test case contains a single integer n (3≤n≤100) — the length of the array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤100).
It is guaranteed that all the numbers except one in the a array are the same.
Output
For each test case, output a single integer — the index of the element that is not equal to others.
Example
Input
4
4
11 13 11 11
5
1 4 4 4 4
10
3 3 3 3 10 3 3 3 3 3
3
20 20 10
Output
2
1
5
3
代码实现:
import java.util.Arrays;
import java.util.Scanner;
public class Spy_Detected {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- != 0) {
int num1 = -1;
int num2 = -1;
int num3 = -1;
int mark1 = -1;
int mark2 = -1;
int mark3 = -1;
int num;
int len = sc.nextInt();
for(int i=0;i<len;i++) {
num = sc.nextInt();
if(num1 == -1) {
num1 = num;
mark1 = i;
}
else if(num2 == -1) {
num2 = num;
mark2 = i;
}
else if(num3 == -1){
if(num1 == num2) {
if(num != num1) {
num3 = num;
mark3 = i;
}
}else {
num3 = num;
mark3 = i;
}
}
}
//mark是下标,输出需要加1
if(num1 == num2) System.out.println(mark3+1);
else {
if(num1 == num3) System.out.println(mark2+1);
else System.out.println(mark1+1);
}
}
}
}