码迷,mamicode.com
首页 > 其他好文 > 详细

选票统计

时间:2019-12-13 14:14:49      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:source   memory   pre   ref   lin   报名   header   struct   多少   

选票统计

Time Limit: 1000 ms Memory Limit: 65536 KiB
某校学生会主席由全校学生投票选举产生,共有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

Hint

 

Source

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct
 4 {
 5     int name,score;
 6 }a[1001]={{0,0}},t;
 7  
 8 int main()
 9 {
10     int m,n,x,i,j;
11     scanf("%d %d",&m,&n);
12     for(i=0;i<=n-1;i++)
13     {
14         scanf("%d",&x);
15         if(x<=m&&x>0)
16         {
17             a[x-1].score++;
18             a[x-1].name=x;
19         }
20     }
21     for(i=0;i<=m-2;i++)
22     {
23         for(j=0;j<=m-2-i;j++)
24         {
25             if(a[j].score<a[j+1].score)
26             {
27                 t=a[j];
28                 a[j]=a[j+1];
29                 a[j+1]=t;
30             }
31         }
32     }
33     printf("%d\n%d\n",a[0].name,a[0].score);
34     return 0;
35 }

选票统计

标签:source   memory   pre   ref   lin   报名   header   struct   多少   

原文地址:https://www.cnblogs.com/xiaolitongxueyaoshangjin/p/12034463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!