Ace of Aces

来源:互联网 发布:淘宝懒人模板 编辑:程序博客网 时间:2024/05/29 00:31

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 numberAi 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".

Input

There are multiple test cases. The first line of input contains an integer Tindicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 1000). The next line contains Nintegers Ai (1 <= Ai <= 1000).

<h4< dd="">
Output

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.

<h4< dd="">
Sample Input
352 2 2 1 151 1 2 2 31998
<h4< dd="">
Sample Output
2Nobody

998

题目大意:先输入一个整数n,表示n个测试数据,其次在数每组测试数据中选的人的标号,输出获得票数最多的那个人,要是最多票数的有2人以上,则输出Nobody;

分析:没啥好分析的,附上代码,挺简单的。

#include <iostream>#include <cstdio>#include <algorithm>#include <string.h>#include <cmath>#include <math.h>using namespace std;int main(){    int n;    int a[1001],s[1001];    scanf("%d",&n);    {        int b;        for(int i=0;i<n;i++)        {            memset(s,0,sizeof(s));            int sum=0,e;            scanf("%d",&b);            int p=0,q=0,c=0;            for(int j=0;j<b;j++)            {                scanf("%d",&a[j]);                s[a[j]]++;                if(s[a[j]]>=p)                {                    q=p;                    p=s[a[j]];                    c=a[j];                }            }            if(p==q)                cout<<"Nobody"<<endl;            else                cout<<c<<endl;//            int t=0;//            for(int j=0;j<=1000;j++)//            {//                t=max(t,s[j]);//            }//            for(int j=0;j<=1000;j++)//            {//                if(t==s[j])//                {//                    sum++;//                    e=j;//                }//            }//            if(sum%2==0)//                printf("Nobody\n");//            else//                printf("%d\n",e);        }    return 0;    }}
注释的那一部分是我一开始做的时候写的,是错误的。原因是在测试 1 2 3的时候sum是1,然后在寻找投票人数是1的时候,sum就是奇数了,所以导致wrong answer了。

没有注释的那个是从网上借鉴来的,这个方法很巧妙,p,q不断刷新,c是存当前投票次数最多的那个人的编号的,也是最终的结果;

这个题重点是掌握a[b[i]]这种计数方法。。



原创粉丝点击