Step By Step 1294 选票统计

选票统计

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

某校学生会主席由全校学生投票选举产生,共有m名候选人报名参选,编号为1到m(0<m<1000),全校有n名学生(0<n<30000),每人都可以投票。但每人只能投一票,每票只能选1名候选人。请你设计一个程序能够统计出哪个候选人得票最高,得了多少票。不会出现得票数相同的情况。

Input

第一行输入候选人数m和学生数n,以空格分开;
下面依次输入每个学生所选的候选人的编号。

Output

第一行输出得票最多的候选人编号;
第二行输出该候选人所得的票数。

Sample Input

3 10
1 2 3 2 3 1 2 3 1 3

Sample Output

3
4

#include<iostream>
#include<algorithm>
using namespace std;


struct man
{
    int num;
    int c=0;
};
bool add (struct man a,struct man b)
{
    return a.c>b.c;
}
int main()
{
    struct man a[1001];
    int i,j,k;
    for(i=0;i<1001;i++)
    {
        a[i].num=i;
        a[i].c=0;
    }
    int m,n;
    cin>>m>>n;
    for(i=0;i<n;i++)
    {
        cin>>k;
        a[k].c++;
    }
    sort(a,a+m+1,add);
    cout<<a[0].num<<endl<<a[0].c<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/80893536