目录
Codeforces Round #713 (Div. 3)-A. Spy Detected!
传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes
Problem Description
You are given an array a a a consisting of n n n ( n ≥ 3 n \ge 3 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 ] [4, 11, 4, 4] [4,11,4,4] all numbers except one are equal to 4 4 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 t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1≤t≤100). Then t t t test cases follow.
The first line of each test case contains a single integer n n n ( 3 ≤ n ≤ 100 3 \le n \le 100 3≤n≤100) — the length of the array a a a.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 100 1 \le a_i \le 100 1≤ai≤100).
It is guaranteed that all the numbers except one in the a a 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.
Sample 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
Sample Onput
2
1
5
3
题目大意
给你 n ( n ≥ 3 ) n(n\geq3) n(n≥3)个数,其中只有一个数字与众不同。让你给出这个与众不同的数字下标。
解题思路
先看数据范围, t < 100 , n < 100 , a i < 100 t<100,n<100,a_i<100 t<100,n<100,ai<100,所以暴力就够了
一个很笨的方法就是输入数据之后,把每个数出现的次数+1,然后遍历一遍,得到出现一次的那个数。
再数组一遍数组,得到这个数的下标。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int a[200];//记录题目中给的数组
int main()
{
int N;
cin>>N;
while(N--)//N组测试样例
{
int n;
cd(n);//scanf("%d", &n);
int sum[200]={0};//记录出现的次数,sum[i]代表i出现的次数
fi(i,0,200)//for(int i=0;i<200;i++)
sum[i]=0;//初始值为0
fi(i,0,n)
{
cd(a[i]);//输入a[i]
sum[a[i]]++;//a[i]出现的次数++
}
int diff=0;//不同的那个数(只出现一次的那个数)
fi(i,1,200)
{
if(sum[i]==1)//这个数只出现了一次
{
diff=i;
break;
}
}
// dbg(diff);
fi(i,0,200)
{
if(a[i]==diff)//如果这个数就是只出现一次的那个数
{
printf("%d\n",i+1);//输出
break;//结束循环
}
}
}
return 0;
}
原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/115592470