题目描述
编写统计选票的程序。设有 333 个候选人,有 101010 人参加选举,每个选举人只能输入一个候选人的编号,要求输出各个候选人的得票数。
输入
输入 101010 个选举人提交的候选人编号
输出
输出候选人编号及对应的得票
输入输出样例
样例输入
1001
1001
1002
1003
1001
1002
1002
1003
1003
1001
样例输出
1001:4
1002:3
1003:3
解答:
#include<stdio.h>
struct person
{
int ID;
int count;
} leader[3] = { 1001,0,1002,0,1003,0 };
int main()
{
int i, j;
struct person num[10];
for (i = 0; i < 10; i++)
{
scanf("%d", &num[i].ID);
for (j = 0; j < 3; j++)
{
if (num[i].ID == leader[j].ID)
leader[j].count++;
}
}
for (i = 0; i < 3; i++)
printf("%d:%d\n",leader[i].ID,leader[i].count);
}