Football (aka Soccer) UVA 10194

说说:万万没想到会在这道题上花费这么多时间。这道题其实就是比较繁琐而已,需要一个比较大的结构记录一个队伍的所有信息。原以为最难的是对比赛结果字符串的提取,但事实证明这倒还挺简单的。关键是后面对比赛结果的排序,原理相对简单,代码较庞大。需要注意的是,最后如果按队名排序是不区分大小写的,并且队名不只是英文字母!而且这里提一下C的语言特性,如果在调用一个返回字符指针的函数,且返回的是被调用函数内定义的字符数组,那样是会出错的,因为子函数调用结束后相应的资源就被释放了。要关注细节呀~哈哈~

题目:

Football (aka Soccer)

The Problem

Football the most popular sport in the world (americans insist to call it "Soccer",but we will call it "Football"). As everyone knows, Brasil is the country that have most World

足球是世界上最受欢迎的运动(美国人坚持叫它“soccer”,但我们叫它“football”)。众所周知,巴西是世界上获得世界杯冠军最多的国家(1958,1962,1970和1994,题目

Cup titles (four of them: 1958, 1962, 1970 and 1994). As our national tournament have many teams(and even regional tournaments have many teams also) it‘s a very hard

有点老....)。因为我们的国家锦标赛有很多队伍(即使区域锦标赛也有很多队伍),所以记录这些队伍和比赛的成绩就显得非常困难了。

task to keep track of standings with so many teams and games played!

So, your task is quite simple: 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. Obviously, a team loses a game if it scores less goals. When both teams score the same number of goals, we

如果一个队伍比对手得了更多分,那么他们将赢得比赛。显然,如果一个队伍得分少,那么他们将输掉比赛。如果两个队伍得了同样的分,

call it a tie.A team earns 3 points for each win, 1 point for each tie and 0 point for each loss.

那两队平局。一个队伍每赢一场得三分,平一场得一分,输一场得0分

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. Less games played.比赛场次最少
  6. Lexicographic order.字典序

The Input

The first line of input will be an integer N in a line alone (0 < N < 1000). Then, will follow N tournament descriptions. Each one begins with the tournament name, on a single

第一行输入是一个整数N(0<N<1000).接下来是关于N场锦标赛的描述。每场的开头一行是锦标赛名称

line. Tournament names can have any letter, digits, spaces etc. Tournament names will have length of at most 100. Then, in the next line, there will be a number T (1 < T <=

锦标赛名称可以有任意多的字母,数字,空格等待。锦标赛名称最多有100个字符那么长。接下来是一个数T(1<T<=30)

30), which stands for the number of teams participating on this tournament. Then will follow T lines, each one containing one team name. Team names may have any

代表参加锦标赛的队伍的数量。接下来T行,每行一个队伍名称。队伍的名称可能包含任何大于32的ASCII码,除了‘#’和‘@’。

character that have ASCII code greater than or equal to 32 (space), except for ‘#‘ and ‘@‘ characters, which will never appear in teamnames. No team name will have more than 30 characters.

并且没有队伍的名称会超过三十个字符。

Following to 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

队伍名称之后是一个非负整数G,代表了锦标赛中已经完成的比赛。

greater than 1000. Then, G lines will follow with the results of games played. They will follow this format:

G不会大于1000.然后接下来的G行是比赛的结果。它们将有如下的形式

team_name_1#[email protected]#team_name_2

For instance, the following line:

Team A#[email protected]#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

意思是在Team A和Team B之间有一场比赛,并且Team A得了3分,Team B得了1分。所有的得分是一个不超过20的非负整数。

there will not be in existent team names (i.e. all team names that appear on game results will have apperead on the team names section) and that no team will play against

可以保证每个比赛结果中出现的队伍在之前的队伍名称环节都出现过并且没有队伍会和自己比赛。

itself.

The output

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. Notice that

对于每场锦标赛,你必须先输出一行队伍名称接下来的T行根据上述规则输出成绩。需要注意的是当出现平局时按照字典排序,此时不区分大小写。

should the tie-breaker be the lexographic order, it must be done case in senstive. The output format for each line is shown bellow:

输出形式如下所示:

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

Sample Input

2
World Cup 1998 - Group A
4
Brazil
Norway
Morocco
Scotland
6
Brazil#[email protected]#Scotland
Norway#[email protected]#Morocco
Scotland#[email protected]#Norway
Brazil#[email protected]#Morocco
Morocco#[email protected]#Scotland
Brazil#[email protected]#Norway
Some strange tournament
5
Team A
Team B
Team C
Team D
Team E
5
Team A#[email protected]#Team B
Team A#[email protected]#Team C
Team A#[email protected]#Team D
Team E#[email protected]#Team C
Team E#[email protected]#Team D

Sample output

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)

源代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef struct team{//一个队伍的所有信息
 char name[35];
 int point;
 int game_played;
 int win,loss,tie;
 int goal_differ,goal_scored,goal_against;
}TEAM;

TEAM teams[30+5];
char tournament[100+5];
char result[100];
int N,T,Q;

void handle_str(char*,char*,char*,int*,int*);//处理比赛结果的字符串
void add_result(char*,char*,int,int);//将比赛结果添加到相应结点
void sort();
void output_result();
char* convert(char*);

int main(){
 int i,j,k;
 char Team_A[35],Team_B[35];
 int score_A,score_B;
 //freopen("D:\data.txt","r",stdin);
 scanf("%d\n",&N);

 while(N--){
    gets(tournament);
    scanf("%d\n",&T);
    for(i=0;i<T;i++){//初始化队伍信息
        gets(teams[i].name);
        teams[i].point=teams[i].game_played=0;
        teams[i].win=teams[i].loss=teams[i].tie=0;
        teams[i].goal_differ=teams[i].goal_scored=teams[i].goal_against=0;
    }

    scanf("%d\n",&Q);
    for(i=0;i<Q;i++){
        gets(result);
        handle_str(result,Team_A,Team_B,&score_A,&score_B);//处理比赛结果
        add_result(Team_A,Team_B,score_A,score_B);//添加比赛结果
    }
    sort();

    output_result();
    if(N) putchar('\n');
 }

 return 0;
}

void output_result(){//输出比赛结果
  int i;

  printf("%s\n",tournament);
  for(i=0;i<T;i++){
    printf("%d) %s %dp, %dg ",i+1,teams[i].name,teams[i].point,teams[i].game_played);
    printf("(%d-%d-%d), ",teams[i].win,teams[i].tie,teams[i].loss);
    printf("%dgd (%d-%d)\n",teams[i].goal_differ,teams[i].goal_scored,teams[i].goal_against);
  }
  return;
}

void add_result(char*Team_A,char*Team_B,int score_A,int score_B){//添加比赛结果
 int i;

 for(i=0;i<T;i++)
 if(strcmp(Team_A,teams[i].name)==0){
    teams[i].game_played++;
    teams[i].goal_differ+=score_A-score_B;
    teams[i].goal_scored+=score_A;
    teams[i].goal_against+=score_B;
    if(score_A>score_B){
        teams[i].point+=3;
        teams[i].win++;
    }
    else if(score_A==score_B){
        teams[i].point+=1;
        teams[i].tie++;
    }
    else
        teams[i].loss++;
 }
 else if(strcmp(Team_B,teams[i].name)==0){
    teams[i].game_played++;
    teams[i].goal_differ+=score_B-score_A;
    teams[i].goal_scored+=score_B;
    teams[i].goal_against+=score_A;
    if(score_B>score_A){
        teams[i].point+=3;
        teams[i].win++;
    }
    else if(score_A==score_B){
        teams[i].point+=1;
        teams[i].tie++;
    }
    else
        teams[i].loss++;
 }

 return ;
}

void handle_str(char* result,char*Team_A,char*Team_B,int*score_A,int*score_B){//处理结果字符串
  int i,pos;
  int A=0,B=0;
  i=pos=0;
  while(result[pos]!='#')
    Team_A[i++]=result[pos++];
  Team_A[i]='\0';

  pos++;
  while(isdigit(result[pos]))
    A=A*10+result[pos++]-'0';
  *score_A=A;

  pos++;
  while(isdigit(result[pos]))
    B=B*10+result[pos++]-'0';
  *score_B=B;

  pos++;
  i=0;
  while(result[pos]!='\0')
    Team_B[i++]=result[pos++];
  Team_B[i]='\0';
}

void sort(){
  int i,j;
  TEAM temp;
  char team_A[35],team_B[35];
  for(i=1;i<T;i++)
  for(j=i;j>0;j--){//按题目所给的规则进行排序
    if(teams[j].point<teams[j-1].point)
        break;
    else if(teams[j].point==teams[j-1].point){
        if(teams[j].win<teams[j-1].win)
        break;
        else if(teams[j].win==teams[j-1].win){
            if(teams[j].goal_differ<teams[j-1].goal_differ)
                break;
            else if(teams[j].goal_differ==teams[j-1].goal_differ){
                if(teams[j].goal_scored<teams[j-1].goal_scored)
                    break;
                else if(teams[j].goal_scored==teams[j-1].goal_scored){
                    if(teams[j].game_played>teams[j-1].game_played)
                      break;
                    else if(teams[j].game_played==teams[j-1].game_played){
                        strcpy(team_A,teams[j].name);
                        strcpy(team_B,teams[j-1].name);
                        if(strcmp(convert(team_A),convert(team_B))>=0)//注意!不区分大小写
                            break;
                    }
                }
            }
        }
    }

    temp=teams[j];
    teams[j]=teams[j-1];
    teams[j-1]=temp;
  }
}

char*convert(char*team){
  int i=0;
  while(team[i]){
    if(isalpha(team[i]))//队名并非都为字母
    team[i]=tolower(team[i]);
    i++;
  }
  return team;
}

Football (aka Soccer) UVA 10194

时间: 2024-11-03 22:37:54

Football (aka Soccer) UVA 10194的相关文章

UVa 10194 - Football (aka Soccer)

题目:给你一些球队的比赛记录,对球队的成绩进行排序. 分析:模拟.按照题意模拟即可. 排序优先级:1.分数高 ,2.胜场多,3.净胜球多,4.进球多,5.比赛场次少,6.队名字典序. 注意:队名大小写不敏感,即大小写认为是相同的. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> using namesp

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

小白书练习题5.5.3 排序检索类、

UVA 340 Master-Mind Hints 题意:猜数字游戏,给n个数的序列给你.接下来一行是答案序列.剩下的都是猜测序列.对于每一个猜测序列,统计有多少个数字相同并且位置相同.有多少数字相同位置不同.每一个数字只能用一次. 思路:直接统计可以求出数字相同并且位置相同的哪一些数.在此过程中我加了一个标记数组.标记那些用过的数的位置为1,没用过为0:然后枚举猜测中哪些没用过的数字.去答案序列中找.当数字相等并且答案行中那个数也没用过时.计数加1: 1 #include<cstdio> 2

计划,,留

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 一.<算法竞赛入门经典> 刘汝佳 (UVaOJ 351道题) 以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html "AOAPC I"

算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发.   一.UVaOJ http://uva.onlinejudge.org  西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ.   二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html   "AO

(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinejudge.org 西班牙Valladolid大学的程序在线评测系统,是历史最悠久.最著名的OJ. 二.<算法竞赛入门经典> 刘汝佳  (UVaOJ  351道题)  以下部分内容摘自:http://sdkdacm.5d6d.com/thread-6-1-1.html “AOAPC I”是刘汝佳(大

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

计算机科学及编程导论(7)数组及可变性、字典、伪代码,代码运行效率简介

1. 数组及可变性 当创建一个数组的时候,它将与一个对象进行绑定 L1 = [1, 2, 3] L2 = L1 L1[0] = 4 print(L2)#=>[4, 2, 3] L2 = L1 意味着L2与L1指向同一个对象,而L1[0]=4则改变了对象的值,所以最终L2的值也会改变,可以与下面这个例子进行比较 a = 1 #a指向对象1 b = a #b指向对象a a = 4 #此时a指向了对象4 print(b) #=>1,由于b依旧指向对象1,所以没有发生变化 2.字典 字典包括了以下几个

表情符号备忘单

表情符号备忘单 此页上列出的emoji 表情图释支持篝火. GitHub, Basecamp. Redbooth. Trac. Flowdock. Sprint.ly. Kandan. Textbox.io. Kippt. Redmine. JabbR.特雷略.大厅. Qiita.创业.红宝石中国.格罗夫. Idobata. NodeBB 论坛.可宽延时间. Streamup. OrganisedMinds. Hackpad. Cryptbin.加藤.据报.开朗鬼. IRCCloud. Dash