Time Limit: 1000MS
Memory Limit: 65536KB
Submit
Statistic
Problem Description
某校学生会主席由全校学生投票选举产生,共有m名候选人报名参选,编号为1到m(0<m<1000),全校有n名学生(0<n<30000),每人都可以投票。但每人只能投一票,每票只能选1名候选人。请你设计一个程序能够统计出哪个候选人得票最高,得了多少票。不会出现得票数相同的情况。
Input
第一行输入候选人数m和学生数n,以空格分开;
下面依次输入每个学生所选的候选人的编号。
Output
第一行输出得票最多的候选人编号;
第二行输出该候选人所得的票数。
Example Input
3 10
1 2 3 2 3 1 2 3 1 3
Example Output
3
4
Hint
Author
//code1
01 #include<stdio.h>
02 #include<stdlib.h>
03 #include<string.h>
04 struct node
05 {
06 int num;
07 }p[1000];
08
09 int main()
10 {
11 int a[1000];
12 int n,m;
13 memset(a,0,sizeof(a));//对数组初始化为零
14 scanf("%d %d",&m,&n);
15 for(int i=1;i<=n;i++)
16 {
17 scanf("%d",&p[i].num);
18 a[p[i].num]++;
19 }
20 int max=0 , k=0;
21 for(int i=1;i<=m;i++)
22 {
23 if(max<a[i])
24 {
25 max=a[i];
26 k=i;
27 }
28 }
29 printf("%d\n%d",k,a[k]);
30 return 0;
31 }
//code2
01 #include<stdio.h>
02 #include<stdlib.h>
03 #include<string.h>
04 struct node
05 {
06 int num ;
07 }p[30000];
08 int main()
09 {
10 int n,m;
11 int count[1000];
12 memset(count,0,sizeof(count));//初始化为零
13 scanf("%d %d",&m,&n);
14 for(int i=0; i<n ; i++)
15 {
16 scanf("%d",&p[i].num);
17 }
18 for(int i=1;i<=m;i++)
19 {
20 for(int j=0; j<n ; j++)
21 {
22 if(p[j].num==i)
23 count[i]++;
24 }
25 }
26 int max=0,k=0;
27 for(int i=1;i<=m;i++)
28 {
29 if(count[i]>max)
30 {
31 max=count[i];
32 k=i;
33 }
34 }
35 printf("%d\n%d",k,count[k]);
36 return 0;
37 }
转载请注明原文地址: https://ju.6miu.com/read-500089.html