首页 > 编程语言 > 详细

UVA10194 Football (aka Soccer)【排序】

时间:2019-02-20 20:22:20      阅读:225      评论:0      收藏:0      [点我收藏+]

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 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 oponent. Obviously, a team loses a game if it scores less goals. hen 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.
????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.

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 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 ≤ 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 character that have ASCII code greater than or equal to 32 (space), except for ‘#’ and ‘@’ characters, which will never appear in team names. 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 greater than 1000. Then, G lines will follow with the results of games played. They will follow this format:
team name 1#goals1@goals2#team name 2
????For instance, the following line:
Team A#3@1#Team B
????Means that in a game between T eam A and T eam B, T eam A scored 3 goals and T eam B scored 1.
????All goals will be non-negative integers less than 20. You may assume that there will not be inexistent 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.
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 should the tie-breaker be the lexographic order, it must be done case insenstive. The output format for each line is shown bellow:
[a]) T eam name [b]p, [c]g ([d]-[e]-[f]), [g]gd ([h]-[i])
Where:
? [a] = team rank
? [b] = total points earned
? [c] = games played
? [d] = wins
? [e] = ties
? [f] = losses
? [g] = goal difference
? [h] = goals scored
? [i] = 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.
Sample Input
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
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)

问题链接UVA10194 Football (aka Soccer)
问题简述:(略)
问题分析
????繁琐的排序题,不解释。
程序说明
????程序中,函数scanf()按正则表达式格式输入,函数strcasecmp()忽略大小写的比较。这两个用法值得推荐,可以大幅简化代码。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10194 Football (aka Soccer) */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
const int TN = 32;
const int T = 30;
struct Team {
    char name[TN + 1];      // 球队名字
    int point;      // 得分
    int wins;       // 胜场数
    int ties;       // 平场数
    int losses;     // 输场数
    int scored;     // 进球
    int against;        // 失球
};

int cmp(const Team& a, const Team& b){
    if(a.point != b.point)      // 得分高的先输出
        return a.point > b.point;
    if(a.wins != b.wins)        // 胜场数高的先输出
        return a.wins > b.wins;
    if((a.scored - a.against) != (b.scored - b.against))        // 净进球数高的先输出
        return (a.scored - a.against) > (b.scored - b.against);
    if(a.scored != b.scored)        // 进球数高的先输出
        return a.scored > b.scored;
    if((a.wins + a.ties + a.losses) != (b.wins + b.ties + b.losses))       // 参与场次少的先输出
        return (a.wins + a.ties + a.losses) < (b.wins + b.ties + b.losses);
    return strcasecmp(a.name, b.name) < 0;     // 队名字典序输出
}

int main()
{
    int n, t, g;
    char name[N + 1];

    scanf("%d", &n);
    getchar();
    while(n--) {
        Team team[T];

        gets(name);

        scanf("%d", &t);
        getchar();
        memset(team, 0, sizeof(team[0]) * t);
        for(int i = 0; i < t; i++)
            gets(team[i].name);

        scanf("%d", &g);
        getchar();
        char team1[TN + 1], team2[TN + 1];
        int g1, g2, t1 = 0, t2 = 0;
        for(int i = 0; i < g; i++) {
            scanf("%[^#]#%d@%d#%[^\n]", team1, &g1, &g2, team2);
            getchar();
            for(int j = 0; j < t; j++) {
                if(strcmp(team1, team[j].name) == 0)
                    t1 = j;
                if(strcmp(team2, team[j].name) == 0)
                    t2 = j;
            }
            team[t1].scored += g1;
            team[t1].against += g2;
            team[t2].scored += g2;
            team[t2].against += g1;

            if (g1 > g2) {
                team[t1].point += 3;
                team[t1].wins++;
                team[t2].losses++;
            } else if (g1 == g2) {
                team[t1].point += 1;
                team[t1].ties++;
                team[t2].point += 1;
                team[t2].ties++;
            } else if (g1 < g2) {
                team[t1].losses++;
                team[t2].point += 3;
                team[t2].wins++;
            }
        }

        sort(team, team + t, cmp);

        printf("%s\n", name);
        for(int i = 0; i < t; i++) {
            printf("%d) ", i + 1);
            printf("%s ", team[i].name);
            printf("%dp, ", team[i].point);
            printf("%dg ", (team[i].wins + team[i].ties + team[i].losses));
            printf("(%d-%d-%d), ", team[i].wins, team[i].ties, team[i].losses);
            printf("%dgd ", (team[i].scored - team[i].against));
            printf("(%d-%d)\n", team[i].scored, team[i].against);
        }
        if(n) printf("\n");
    }
    return 0;
}

UVA10194 Football (aka Soccer)【排序】

原文:https://www.cnblogs.com/tigerisland45/p/10408906.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!