投票选出冠军
#include<stdio.h>
#include<string.h>
#define M 100
#define N 30
typedef struct
{
char code[8];
int score;
}st;
typedef struct
{
char code[8];
int score;
}st1;
main()
{
st candidates[N],k;
st1 tmp[N];
int i,j,t,n;
printf("请输入选手个数:");
scanf("%d",&t);
for(i=1;i<=t;++i)
{
printf("输入第 %d 个候选人的代号:",i);
scanf("%s",&candidates[i].code);
candidates[i].score=0;
}
printf("输入投票人数:");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
printf("输入第 %d 张选票:",i);
scanf("%s",&tmp[i].code);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=t;j++)
{
if(!strcmp(tmp[i].code,candidates[j].code))
candidates[j].score++;
}
}
printf("\n--------------------\n");
for(i=1;i<=t;++i)
{
printf("%s得分为%d\n",candidates[i].code,candidates[i].score);
}
for(j=1;j<=t-1;j++)
{
for(i=1;i<=t-j;i++)
{
if(candidates[i].score>candidates[i+1].score)
{
k=candidates[i+1];
candidates[i+1]=candidates[i];
candidates[i]=k;
}
}
}
printf("\n--------------------\n");
for(j=t-1;j>=t-1;j--)
{
if(candidates[j].score==candidates[t].score)
{
printf("\n冠军不唯一,请重新投票\n");
}
else
printf("冠军为%s得分为%d\n",candidates[t].code,candidates[t].score);
}
}