UVA - 10194 Football (aka Soccer) 点击打开链接
从这道题中收获蛮多,首先又一次知道自己的代码风格一点都不好,乱七八糟的,还有也就是抓不住题目重点,导致不停WA。
不过知道了还有strcasecmp这个函数,不区分大小写排序,并且从别人代码中学习风格,让程序清晰很多。
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
char name[100];
int p,g,w,l,t,gd,gs,ga; //得分 场数 胜 负 平 净胜球 进球数 失球数
};
int cmp(const void *a,const void *b)
{
struct node *c=(node *)a;
struct node *d=(node *)b;
if(c->p!=d->p) return d->p - c->p;
else if(c->w!=d->w) return d->w - c->w;
else if(c->gd!=d->gd) return d->gd - c->gd;
else if(c->gs!=d->gs) return d->gs - c->gs;
else if(c->g!=d->g) return c->g - d->g;
else return strcasecmp(c->name,d->name);
}
int n,m;
int find(char a[],node *team)
{
for(int j=0;j<n;j++)
if(strcmp(team[j].name,a)==0) return j;
}
int main()
{
//freopen("a.txt","r",stdin);
int t,g1,g2;
char match[3001];
char team1[3500],team2[3500];
scanf("%d",&t);
getchar();
while(t--)
{
node team[5001]={0};
gets(match);
puts(match);
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
gets(team[i].name);
scanf("%d",&m);
getchar();
for(int i=0;i<m;i++)
{
int t1=0;
int a1,a2;
char c;
while(1)
{
scanf("%c",&c);
if(c=='#') break;
team1[t1++]=c;
}
team1[t1]='\0';
scanf("%d%c%d%c",&g1,&c,&g2,&c);
gets(team2);
a1=find(team1,team);
a2=find(team2,team);
team[a1].gs+=g1;
team[a1].ga+=g2;
team[a1].g++;
team[a2].gs+=g2;
team[a2].ga+=g1;
team[a2].g++;
if(g1>g2)
{
team[a1].p+=3;
team[a1].w++;
team[a2].l++;
team[a1].gd+=(g1-g2);
team[a2].gd+=(g2-g1);
}
else if(g1==g2)
{
team[a1].t++;
team[a2].t++;
team[a1].p++;
team[a2].p++;
}
else
{
team[a2].w++;
team[a1].l++;
team[a2].p+=3;
team[a2].gd+=(g2-g1);
team[a1].gd+=(g1-g2);
}
}
qsort(team,n,sizeof(team[0]),cmp);
for(int i=0;i<n;i++)
printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,team[i].name,team[i].p,team[i].g,team[i].w,team[i].t,team[i].l,team[i].gd,team[i].gs,team[i].ga);
if(t) printf("\n");
}
return 0;
}