有3个候选人,每个选民只能投票选一人,编写一个统计选票的程序,先后输入被选人的名字,最后输出每个人得票结果:
#include<stdio.h>
#include<string.h>
struct peo
{
char name[20];
int count;
}leader[3] = {"zz",0,"hh",0,"ll",0};
int main()
{
char tmp[20] = {0};
int i = 0;
int j = 0;
for (i = 0; i < 10; i++)
{
scanf("%s", tmp);
for (j = 0; j < 3; j++)
{
if (strcmp(tmp,leader[j].name ) == 0)
leader[j].count++;
}
}
for (i = 0; i < 3; i++)
{
printf("%s %d\n", leader[i].name, leader[i].count);
}
return 0;
}
输出结果: