CF 19A World Football Cup

注意字符串大小的比较及结构体的二次排序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define size 60
struct node{
	int goal,mgoal,point,dif;
	char name[35];
}team[size];
int n;
int find(char na[])
{
	int i;
	for(i=0;i<n;i++)
	{
		if(strcmp(na,team[i].name)==0)
			return i;
	}
	return -1;
}
int cmp2(const void*a,const void*b)
{
	node*c=(node*)a;
	node*d=(node*)b;
	return strcmp(c->name,d->name);
}

int cmp(const void*a,const void*b)
{
	node*c=(node*)a;
	node*d=(node*)b;
	if(c->point!=d->point)
		return(c->point < d->point)?1:-1;
	else 
	{
		if(c->dif != d->dif)
			return(c->dif < d->dif)?1:-1;
		else
		{
			return (c->goal < d->goal)?1:-1;
		}
	}
}
int main()
{
	int i,j,k,l,m;
	while(scanf("%d",&n)!=EOF)
	{
		getchar();
		memset(team,0,sizeof(team));
		for(i=0;i<n;i++)
		{
			gets(team[i].name);
		}
		for(i=0;i<n*(n-1)/2;i++)
		{
			char na[35]="",nb[35]="";
			int a,b;
			char t[65]="";
			scanf("%s",&t);
			int g;
			for(j=0;j<strlen(t);j++)
			{
				if(t[j]=='-')
				{
					g=j;
					break;
				}
			}
			for(j=0;j<g;j++)
			{
				na[j]=t[j];
			}
			na[j+1]='\0';
			for(j=g+1;j<strlen(t);j++)
			{
				nb[j-g-1]=t[j];
			}
			nb[strlen(t)-g]='\0';

			a=find(na);
			b=find(nb);
			int ga,gb;
			scanf("%d:%d",&ga,&gb);
			team[a].goal+=ga;
			team[b].goal+=gb;
			team[a].mgoal+=gb;
			team[b].mgoal+=ga;
		
			if(ga!=gb)
			{
				if(ga>gb)
					team[a].point+=3;
				else
					team[b].point+=3;

			}
			else
			{
				team[a].point++;
				team[b].point++;
			}
		}
		for(i=0;i<n;i++)
		{
			team[i].dif=team[i].goal-team[i].mgoal;
		}
		qsort(team,n,sizeof(team[0]),cmp);
		qsort(team,n/2,sizeof(team[0]),cmp2);
		for(i=0;i<n/2;i++)
		{
			puts(team[i].name);
		}
	}
	return 0;
}


你可能感兴趣的:(CF 19A World Football Cup)