代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 1000
typedef struct _team
{
char name[52];
int jf;//积分
int zdf;//总得分
int zsf;//总失分
int rank; //排名
}Team;
//根据名字查找队伍所在数组中的下标
int findTeam(Team team[],int n,char* name)
{
int i=0;
for(;i<n;i++)
{
if(strcmp(team[i].name,name)==0)
return i;
}
return -1;
}
//从读取的一行中解析出队名和比分
void parse(char* str,char name1[],char name2[],int* s1,int* s2)
{
int i=0,j=0;
int nmb = 0;
while(str[i]!='\0' && str[i]==' ')
i++; //过滤前面的空格--以防万一
//获取name1
while(str[i]!='\0' && str[i]!='-')
name1[j++] = str[i++];
//如有空格,删除
name1[j] = 0;
j--;
while(j>=0 && name1[j] ==' ')
{
name1[j] = 0;
j--;
}
i++;
//获取name2
while(str[i]!='\0' && str[i]==' ')
i++; //过滤前面的空格--以防万一
j=0;
while(str[i]!='\0' && str[i]!=':')
name2[j++] = str[i++];
name2[j]=0;
j--;
while(j>=0 && name2[j] ==' ')
{
name2[j] = 0;
j--;
}
//过滤空格等字符,直到遇到数字为止
while(str[i]!='\0' && (str[i]<'0' || str[i]>'9'))
i++;
//得到s1
nmb = 0;
while(str[i]!='\0' && str[i]>='0' && str[i]<='9')
{
nmb = nmb*10 + (str[i]-'0');
i++;
}
*s1 = nmb;
//过滤s2前面的字符
while(str[i]!='\0' && (str[i]<'0' || str[i]>'9'))
i++;
//得到s2
nmb = 0;
while(str[i]!='\0' && str[i]>='0' && str[i]<='9')
{
nmb = nmb*10 + (str[i]-'0');
i++;
}
*s2 = nmb;
}
//排序
void bubbleSort(Team team[],int n)
{
int i,j;
Team t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(team[j].jf < team[j+1].jf) //根据积分排序
{
t = team[j];
team[j] = team[j+1];
team[j+1] = t;
}else if(team[j].jf == team[j+1].jf && ((team[j].zdf-team[j].zsf) < (team[j+1].zdf-team[j+1].zsf)) )
{ //积分相等,根据净得分排序
t = team[j];
team[j] = team[j+1];
team[j+1] = t;
}
}
}
}
//排名
void rank(Team team[],int n)
{
int i=0;
int rk = 1; //初始排名
team[0].rank = 1;
for(i=1;i<n;i++)
{
if(team[i].jf == team[i-1].jf && (team[i].zdf-team[i].zsf) == (team[i-1].zdf-team[i-1].zsf) )
team[i].rank = team[i-1].rank;
else
team[i].rank = i+1;
}
//显示排名
//for(i=0;i<n;i++)
// printf("%d %s %d %d\n",team[i].rank,team[i].name,team[i].jf,(team[i].zdf-team[i].zsf));
//输出获奖队伍
for(i=0;i<n;i++)
{
if(team[i].rank <= n/2 )
printf("%s\n",team[i].name);
}
}
int main()
{
Team team[MAXSIZE];
int i,n;
char buf[200];
char name1[52],name2[52];//队伍名称
int s1,s2; //比分
int index1,index2;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",team[i].name);
team[i].jf = 0;
team[i].zdf = 0;
team[i].zsf = 0;
team[i].rank = 0;
}
getchar(); //吸收回车符
for(i=0;i<n*(n-1)/2;i++)
{
gets(buf); //读取一行
parse(buf,name1,name2,&s1,&s2);
index1 = findTeam(team,n,name1);
index2 = findTeam(team,n,name2);
team[index1].zdf += s1;
team[index1].zsf += s2;
team[index2].zdf += s2;
team[index2].zsf += s1;
if(s1>s2) //name1胜利
team[index1].jf += 3;//积3分
else if(s1 == s2) //平均
{
team[index1].jf += 1;
team[index2].jf += 1;
}else
team[index2].jf += 3; //name2胜利
}
//排序
bubbleSort(team,n);
//排名
rank(team,n);
return 0;
}