Input
There are multiply cases.
For each case,there is a single integer n(1<=n<=1000) in first line.
In second line,there are n integer a1,a2...an(0<=ai<10000)ai is the the ith man's ID.
For each case,there is a single integer n(1<=n<=1000) in first line.
In second line,there are n integer a1,a2...an(0<=ai<10000)ai is the the ith man's ID.
Output
Output ID of the man who should be punished.
If nobody should be punished,output -1.
If nobody should be punished,output -1.
Sample Input
3 1 1 2 4 2 1 4 3
Sample Output
1 -1
简单的小模拟(竟然wa了。。读题啊!)
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10005;
int n, a[maxn];
int main()
{
while(scanf("%d", &n) != EOF) {
memset(a, 0, sizeof(a));
for(int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
a[x]++;
}
int m = 0, flag, sum = 0;
for(int i = 0; i < maxn; ++i) {
if(a[i] > m) {
m = a[i];
flag = i;
}
}
if(m == 1) {
printf("-1\n");
continue;
}
for(int i = 0; i < maxn; ++i)
if(i != flag)
sum += a[i];
if(m > sum) printf("%d\n", flag);
else printf("-1\n");
}
return 0;
}