http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5493
The 12th Zhejiang Provincial Collegiate Programming Contest - A
There is a mysterious organization called Time-Space Administrative Bureau (TSAB) in the deep universe that we humans have not discovered yet. This year, the TSAB decided to elect an outstanding member from its elite troops. The elected guy will be honored with the title of "Ace of Aces".
After voting, the TSAB received N valid tickets. On each ticket, there is a number Ai denoting the ID of a candidate. The candidate with the most tickets nominated will be elected as the "Ace of Aces". If there are two or more candidates have the same number of nominations, no one will win.
Please write program to help TSAB determine who will be the "Ace of Aces".
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer N (1 <= N <= 1000). The next line contains N integers Ai (1 <= Ai <= 1000).
For each test case, output the ID of the candidate who will be honored with "Ace of Aces". If no one win the election, output "Nobody" (without quotes) instead.
3 5 2 2 2 1 1 5 1 1 2 2 3 1 998
2 Nobody 998
分析:
这道题应该是这次浙江省acm省赛的签到题了.
就是比较整数数组元素出现次数最多的一个,如果有多个 输出 Nobody
AC代码:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <iostream> 4 #include <string.h> 5 #include <string> 6 #include <math.h> 7 #include <stdlib.h> 8 #include <queue> 9 #include <stack> 10 #include <set> 11 #include <map> 12 #include <list> 13 #include <iomanip> 14 #include <vector> 15 #pragma comment(linker, "/STACK:1024000000,1024000000") 16 #pragma warning(disable:4786) 17 18 using namespace std; 19 20 const int INF = 0x3f3f3f3f; 21 const int MAX = 1000 + 10; 22 const double eps = 1e-8; 23 const double PI = acos(-1.0); 24 25 int a[MAX]; 26 27 int main() 28 { 29 int T; 30 while(~scanf("%d",&T)) 31 { 32 while(T --) 33 { 34 memset(a , 0 , sizeof(a)); 35 int ma = -INF , i , temp; 36 int n; 37 cin >> n; 38 while(n --) 39 { 40 scanf("%d",&temp); 41 a[temp] ++; 42 ma = max(ma , temp); 43 } 44 int MA = -INF; 45 int ans = 1; 46 for( i = 1;i <= ma;i ++) 47 { 48 if(MA < a[i]) 49 { 50 MA = a[i]; 51 ans = i; 52 } 53 } 54 int ans1 = 0; 55 for(i = 1;i <= ma;i ++) 56 if(MA == a[i]) 57 ans1 ++; 58 if(ans1 == 1) 59 cout << ans << endl; 60 else 61 cout << "Nobody" << endl; 62 } 63 } 64 return 0; 65 }