本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
Codeforces Round #713 (Div. 3)-A. Spy Detected!
传送门 Time Limit: 2 seconds Memory Limit: 256 megabytes
Problem Description
You are given an array consisting of () positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array all numbers except one are equal to ).
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 (). Then test cases follow.
The first line of each test case contains a single integer () — the length of the array .
The second line of each test case contains integers ().
It is guaranteed that all the numbers except one in the 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
题目大意
给你个数,其中只有一个数字与众不同。让你给出这个与众不同的数字下标。
解题思路
先看数据范围,,所以暴力就够了
一个很笨的方法就是输入数据之后,把每个数出现的次数+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;
}
同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…