Spy Detected

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);
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值