Football (aka Soccer)

Football (aka Soccer)

时间: 1ms        内存:64M

描述:

Football is the most popular sport in the world, even through Americans insist on calling it ``soccer." A country such as five-time World Cup-winning Brazil has so many national and regional tournaments that is it very difficult to keep track. Your task is to write a program that receives the tournament name, team names, and games played and outputs the tournament standings so far.

A team wins a game if it scores more goals than its opponent, and loses if it scores fewer goals. Both teams tie if they score the same number of goals. A team earns 3 points for each win, 1 point for each tie, and 0 points for each loss.

Teams are ranked according to these rules (in this order):

1.Most points earned.
2.Most wins.
3.Most goal difference (i.e., goals scored - goals against)
4.Most goals scored.
5.Fewest games played.
6.Case-insensitive lexicographic order

输入:

The first line of input will be an integer N in a line alone ( 0 < N < 1, 000). Then follow N tournament descriptions, each beginning with a tournament name. These names can be any combination of at most 100 letters, digits, spaces, etc., on a single line. The next line will contain a number T ( 1 < T30), which stands for the number of teams participating on this tournament. Then follow T lines, each containing one team name. Team names consist of at most 30 characters, and may contain any character with ASCII code greater than or equal to 32 (space), except for ``#'' and ``@'' characters. Following the team names, there will be a non-negative integer G on a single line which stands for the number of games already played on this tournament. G will be no greater than 1,000. G lines then follow with the results of games played in the format: team_name_1#goals1@goals2#team_name_2 For instance, Team A#3@1#Team B means that in a game between Team A and Team B, Team A scored 3 goals and Team B scored 1. All goals will be non-negative integers less than 20. You may assume that all team names mentioned in game results will have appeared in the team names section, and that no team will play against itself.

输出:

For each tournament, you must output the tournament name in a single line. In the next T lines you must output the standings, according to the rules above. Should lexicographic order be needed as a tie-breaker, it must be done in a case-insensitive manner. The output format for each line is shown below:

[a]) Team [b]p, [c]g ([d]-[e]-[f]), [g]gd ([h]-[i])

where [a] is team rank, [b] is the total points earned, [c] is the number of games played, [d] is wins, [e] is ties, [f] is losses, [g] is goal difference, [h] is goals scored, and [i] is goals against.

There must be a single blank space between fields and a single blank line between output sets. See the sample output for examples.

示例输入:

2
World Cup 1998 - Group A
4
Brazil
Norway
Morocco
Scotland
6
Brazil#2@1#Scotland
Norway#2@2#Morocco
Scotland#1@1#Norway
Brazil#3@0#Morocco
Morocco#3@0#Scotland
Brazil#1@2#Norway
Some strange tournament
5
Team A
Team B
Team C
Team D
Team E
5
Team A#1@1#Team B
Team A#2@2#Team C
Team A#0@0#Team D
Team E#2@1#Team C
Team E#1@2#Team D

示例输出:

World Cup 1998 - Group A
1) Brazil 6p, 3g (2-0-1), 3gd (6-3)
2) Norway 5p, 3g (1-2-0), 1gd (5-4)
3) Morocco 4p, 3g (1-1-1), 0gd (5-5)
4) Scotland 1p, 3g (0-1-2), -4gd (2-6)

Some strange tournament
1) Team D 4p, 2g (1-1-0), 1gd (2-1)
2) Team E 3p, 2g (1-0-1), 0gd (3-3)
3) Team A 3p, 3g (0-3-0), 0gd (3-3)
4) Team B 1p, 1g (0-1-0), 0gd (1-1)
5) Team C 1p, 2g (0-1-1), -1gd (3-4)

提示:

参考答案(内存最优[804]):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct Teams
{
        char name[31];
        int e,gn;
        int w,tie,gl;
        int gc,ga,gd;
};
int N,T,Gp;
char tour[101];
int Found(char *st,Teams *tm)
{
    for(int i=0;i<N;i++)
       if(strcmp(st,tm[i].name)==0)
          return i;
}
bool compare(Teams a,Teams b)
{
     if(a.e>b.e) return true;
     else if(a.e<b.e) return false;
     
     if(a.w>b.w) return true;
     else if(a.w<b.w) return false;
     
     if(a.gd>b.gd) return true;
     else if(a.gd<b.gd) return false;
     
     if(a.gc>b.gc) return true;
     else if(a.gc<b.gc) return false;
     
     if(a.gl<b.gl) return true;
     else if(a.gl>b.gl) return false;
     
     if(a.gn>b.gn) return true;
     else if(a.gn<b.gn) return false;
     
     char s1[31]={'\0'},s2[31]={'\0'};
     for(int i=0;i<strlen(a.name);i++) s1[i]=toupper(a.name[i]);
     for(int i=0;i<strlen(b.name);i++) s2[i]=toupper(b.name[i]);
     if(strcmp(s1,s2)<0) return true;
     else if(strcmp(s1,s2)>0) return false;
     
     return true;
}
void Qsort(Teams *a,int x,int y)
{
     if(x>=y) return;
     int p=x,q=y;
     Teams F=a[p];
     while(p<q)
     {
        while(p<q &&!compare(a[q],F)) q--;
        if(p<q) a[p++]=a[q];
        while(p<q && compare(a[p],F)) p++;
        if(p<q) a[q--]=a[p];
     }
     a[p]=F;
     Qsort(a,x,p-1);
     Qsort(a,p+1,y);
}
void Ranklist(Teams *tm,int N)
{
     for(int i=0;i<N;i++)
        printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,tm[i].name,tm[i].e,tm[i].gn,tm[i].w,tm[i].tie,tm[i].gl,tm[i].gd,tm[i].gc,tm[i].ga);
}
void Complete()
{
     Teams tm[31]={0};
     char team1[101],team2[101],c;
     int g1,g2,t1,t2;
     int ad1,ad2;
     gets(tour); printf("%s\n",tour);
     scanf("%d\n",&N);
     for(int i=0;i<N;i++) gets(tm[i].name);
     scanf("%d\n",&Gp);
     for(int i=0;i<Gp;i++)
     {
        memset(team1,0,sizeof(team1));
        memset(team2,0,sizeof(team2));
        t1=t2=0;
        while(true)
        {
           scanf("%c",&c);
           if(c=='#') break;
           team1[t1++]=c;
        }
        scanf("%d%c%d%c",&g1,&c,&g2,&c);
        gets(team2);
        ad1=Found(team1,tm);
        ad2=Found(team2,tm);
        if(g1>g2)
        {
           tm[ad1].e+=3;
           tm[ad1].w++; tm[ad2].gl++;//Wins,Losses
        }else
        if(g1<g2)
        {
           tm[ad2].e+=3;
           tm[ad2].w++; tm[ad1].gl++;//Wins,Losses
        }else
        {
           tm[ad1].e++;   tm[ad2].e++;
           tm[ad1].tie++; tm[ad2].tie++;//Ties
        }
        tm[ad1].gn++;   tm[ad2].gn++;   //Games Played
        tm[ad1].gc+=g1; tm[ad1].ga+=g2; //Goals Scored,Goals Scored Against
        tm[ad2].gc+=g2; tm[ad2].ga+=g1; //Goals Scored,Goals Scored Against
     }
     for(int i=0;i<N;i++) tm[i].gd=tm[i].gc-tm[i].ga; //Goal Difference
     Qsort(tm,0,N-1);
     Ranklist(tm,N);
}
int main()
{
    scanf("%d\n",&T);
    for(int i=0;i<T;i++)
    {
      if(i) printf("\n");
      Complete();
    }
    return 0;
}

参考答案(时间最优[4]):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct Teams
{
        char name[31];
        int e,gn;
        int w,tie,gl;
        int gc,ga,gd;
};
int N,T,Gp;
char tour[101];
int Found(char *st,Teams *tm)
{
    for(int i=0;i<N;i++)
       if(strcmp(st,tm[i].name)==0)
          return i;
}
bool compare(Teams a,Teams b)
{
     if(a.e>b.e) return true;
     else if(a.e<b.e) return false;
     
     if(a.w>b.w) return true;
     else if(a.w<b.w) return false;
     
     if(a.gd>b.gd) return true;
     else if(a.gd<b.gd) return false;
     
     if(a.gc>b.gc) return true;
     else if(a.gc<b.gc) return false;
     
     if(a.gl<b.gl) return true;
     else if(a.gl>b.gl) return false;
     
     if(a.gn>b.gn) return true;
     else if(a.gn<b.gn) return false;
     
     char s1[31]={'\0'},s2[31]={'\0'};
     for(int i=0;i<strlen(a.name);i++) s1[i]=toupper(a.name[i]);
     for(int i=0;i<strlen(b.name);i++) s2[i]=toupper(b.name[i]);
     if(strcmp(s1,s2)<0) return true;
     else if(strcmp(s1,s2)>0) return false;
     
     return true;
}
void Qsort(Teams *a,int x,int y)
{
     if(x>=y) return;
     int p=x,q=y;
     Teams F=a[p];
     while(p<q)
     {
        while(p<q &&!compare(a[q],F)) q--;
        if(p<q) a[p++]=a[q];
        while(p<q && compare(a[p],F)) p++;
        if(p<q) a[q--]=a[p];
     }
     a[p]=F;
     Qsort(a,x,p-1);
     Qsort(a,p+1,y);
}
void Ranklist(Teams *tm,int N)
{
     for(int i=0;i<N;i++)
        printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,tm[i].name,tm[i].e,tm[i].gn,tm[i].w,tm[i].tie,tm[i].gl,tm[i].gd,tm[i].gc,tm[i].ga);
}
void Complete()
{
     Teams tm[31]={0};
     char team1[101],team2[101],c;
     int g1,g2,t1,t2;
     int ad1,ad2;
     gets(tour); printf("%s\n",tour);
     scanf("%d\n",&N);
     for(int i=0;i<N;i++) gets(tm[i].name);
     scanf("%d\n",&Gp);
     for(int i=0;i<Gp;i++)
     {
        memset(team1,0,sizeof(team1));
        memset(team2,0,sizeof(team2));
        t1=t2=0;
        while(true)
        {
           scanf("%c",&c);
           if(c=='#') break;
           team1[t1++]=c;
        }
        scanf("%d%c%d%c",&g1,&c,&g2,&c);
        gets(team2);
        ad1=Found(team1,tm);
        ad2=Found(team2,tm);
        if(g1>g2)
        {
           tm[ad1].e+=3;
           tm[ad1].w++; tm[ad2].gl++;//Wins,Losses
        }else
        if(g1<g2)
        {
           tm[ad2].e+=3;
           tm[ad2].w++; tm[ad1].gl++;//Wins,Losses
        }else
        {
           tm[ad1].e++;   tm[ad2].e++;
           tm[ad1].tie++; tm[ad2].tie++;//Ties
        }
        tm[ad1].gn++;   tm[ad2].gn++;   //Games Played
        tm[ad1].gc+=g1; tm[ad1].ga+=g2; //Goals Scored,Goals Scored Against
        tm[ad2].gc+=g2; tm[ad2].ga+=g1; //Goals Scored,Goals Scored Against
     }
     for(int i=0;i<N;i++) tm[i].gd=tm[i].gc-tm[i].ga; //Goal Difference
     Qsort(tm,0,N-1);
     Ranklist(tm,N);
}
int main()
{
    scanf("%d\n",&T);
    for(int i=0;i<T;i++)
    {
      if(i) printf("\n");
      Complete();
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注