题目描述
2048星球要举行一次选举,以选出他们的新一任国王。请你帮忙写一道小程序来帮助他们确定谁是新的国王。有三个候选人,每个选民只能投一票,要求输出最终的票最高的人的名字。
输入描述
前三行输入三个候选人的名字,接下来有多组输入,第i+3行代表第i个选民所支持的候选人的名字。
输出描述
输出最终当选的候选人名字。
样例输入:
yangguang
Bob
zhengyang
Bob
Bob
Zhengyang
Yangguang
Bob
样例输出:
Bob
#include<stdio.h>
int main()
{
char candidate[100][100];
scanf("%s",candidate[0]);
scanf("%s",candidate[1]);
scanf("%s",candidate[2]);
int i;
for(i=3;i<8;i++)
{
scanf("%s",candidate[i]);
}
int count1=0,count2=0,count3=0;
for(i=3;i<8;i++) {
if (*candidate[i]==*candidate[0])
{
count1+=1;
}
else if(*candidate[i]==*candidate[1])
{
count2+=1;
}
else if(*candidate[i]==*candidate[2])
{
count3+=1;
}
}
if(count1>=count2&&count1>=count3)
{
printf("%s",candidate[0]);
}
else if(count2>=count3&&count2>=count1)
{
printf("%s",candidate[1]);
}
else if(count3>=count2&&count3>=count1)
{
printf("%s",candidate[2]);
}
return 0;
}