排序/查找 10194 - Football (aka Soccer)

本篇博客介绍了一个UVaOJ上的足球赛事排名系统实现方案,该系统能够根据比赛结果对参赛队伍进行综合排名,涉及字符串处理及自定义排序规则。

UVa OJ

 Problem A: 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 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. 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.

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

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 should the tie-breaker be the lexographic order, it must be done case insenstive. The output format for each line is shown bellow:

[a]) Team_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)

题目比较长,按照样例来解释一下。

首先是输入有多组测试数据。 每组数据当中有n支队伍,接下来给你n行,每行代表一支队伍。之后是T场比赛 ,每场比赛的格式为:Team E#1@2#Team D 即Team E 1:2 Team D

规定没胜一场积分加3,平局加1,输了不加分

之后就是按照给定的规则对这些球队进行排序

先按照分数高低排序,然后按照胜的场次排,然后是净胜球(即总进球数减去丢球数),然后是总进球数,然后是比赛的场次(越少排越前面),最后是按照球队名称按照字典顺序排,不分大小写


其实这道题目的思想很简单,就是字符串处理+给定规则排序。不过处理起来比较麻烦。然后我学习了别人的写法,其中用到了STL中的map(我还一直没有去看,发现很好用!)还有strcasecmp(不分字母大小写进行比较),sscanf()


我自己敲了一遍,wa了很多次,后来发现是输出时多了个逗号。。。。。。。。悲剧

这里不写关于sscanf(),STL (map)了,毕竟是基础,准备另外写一篇。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<ctype.h>
#include<map>
using namespace std;
struct node
{
    char name[50];
    int score,played,wins,ties,loses,in,out;
    void fun(int a,int b)
    {
        played++;
        if (a>b)
        {
            wins++;
            score+=3;
        }
        else if (a==b)
        {
            ties++;
            score+=1;
        }
        else loses++;
        in+=a; out+=b;
    }
} team[40];
bool cmp(node s, node v)
{
    if (s.score!=v.score) return s.score>v.score;
    else if (s.wins!=v.wins) return s.wins>v.wins;
    else if ((s.in-s.out)!=(v.in-v.out)) return ((s.in-s.out)>(v.in-v.out));
    else if (s.in!=v.in) return (s.in>v.in);
    else if (s.played!=v.played) return (s.played<v.played);
    else return strcasecmp(s.name,v.name)<0;//strcasecmp函数,在比较两个字符串时,忽略大小写
}
int main ()
{
    int n,i,j,t,k;
    cin>>n;
    getchar();
    for (k=0; k<n; k++)
    {
        char tour[110];
        map<string,int>games; //定义一个map,以string为关键字
        memset(team,0,sizeof(team));
        gets(tour);
        cin>>t;
        getchar();
        char temp[50];
        for (i=0; i<t; i++)
        {
            gets(temp);
            games.insert(pair<string,int> (temp,i));//往map里面添加数据
            strcpy(team[i].name,temp);
        }
        int g;
        cin>>g;
        getchar();
        char str[1000];
        while(g--)
        {
            char name1[50], name2[50];
            int score1,score2;
            gets(str);
            sscanf(str,"%[^#]#%d@%d#%[^\n]",name1,&score1,&score2,name2);//sscanf函数,可以分割字符串,跳过忽略的字符
            //printf("%s %d %d %s\n",name1,score1,score2,name2);
            team[games[name1]].fun(score1,score2);//games[name1]是调用了map函数,通过name1找到了对应的int
            team[games[name2]].fun(score2,score1);//结构体中还可以定义函数,把程序简单化
        }
        sort(team,team+t,cmp);
        if (k!=0) cout<<endl;
        puts(tour);
        for (i=0; i<t; i++)
            printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",i+1,team[i].name,team[i].score,team[i].played,team[i].wins,team[i].ties,team[i].loses,team[i].in-team[i].out,team[i].in,team[i].out);//之前在输出的时候多了个,。。wa了好多次
    }
    return 0;
}


Windows PowerShell 版权所有(C) Microsoft Corporation。保留所有权利。 安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows PS C:\Users\lai19\IdeaProjects\demo> mvn help:effective-settings WARNING: A terminally deprecated method in sun.misc.Unsafe has been called WARNING: sun.misc.Unsafe::staticFieldBase has been called by com.google.inject.internal.aop.HiddenClassDefiner (file:/E:/maven/apache-maven-3.9.11/lib/guice-5.1.0-classes.jar) WARNING: Please consider reporting this to the maintainers of class com.google.inject.internal.aop.HiddenClassDefiner WARNING: sun.misc.Unsafe::staticFieldBase will be removed in a future release [WARNING] [WARNING] Some problems were encountered while building the effective settings [WARNING] Unrecognised tag: 'mirror' (position: START_TAG seen ...be the preferred\n | server for that repository.\n |-->\n<mirror>... @147:9) @ E:\maven\apache-maven-3.9.11\conf\settings.xml, line 147, column 9 [WARNING] [INFO] Scanning for projects... Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/build-helper-maven-plugin/3.6.1/build-helper-maven-plugin-3.6.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/build-helper-maven-plugin/3.6.1/build-helper-maven-plugin-3.6.1.pom (8.1 kB at 2.5 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/mojo-parent/91/mojo-parent-91.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/mojo-parent/91/mojo-parent-91.pom (38 kB at 37 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/junit/junit-bom/5.13.0/junit-bom-5.13.0.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/junit/junit-bom/5.13.0/junit-bom-5.13.0.pom (5.6 kB at 8.0 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/build-helper-maven-plugin/3.6.1/build-helper-maven-plugin-3.6.1.jar Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/build-helper-maven-plugin/3.6.1/build-helper-maven-plugin-3.6.1.jar (72 kB at 7.6 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/cyclonedx/cyclonedx-maven-plugin/2.9.1/cyclonedx-maven-plugin-2.9.1.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/cyclonedx/cyclonedx-maven-plugin/2.9.1/cyclonedx-maven-plugin-2.9.1.pom (19 kB at 4.5 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/junit/junit-bom/5.11.2/junit-bom-5.11.2.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/junit/junit-bom/5.11.2/junit-bom-5.11.2.pom (5.6 kB at 3.2 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/cyclonedx/cyclonedx-maven-plugin/2.9.1/cyclonedx-maven-plugin-2.9.1.jar Downloaded from central: https://repo.maven.apache.org/maven2/org/cyclonedx/cyclonedx-maven-plugin/2.9.1/cyclonedx-maven-plugin-2.9.1.jar (52 kB at 5.3 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/flywaydb/flyway-maven-plugin/11.7.2/flyway-maven-plugin-11.7.2.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/flywaydb/flyway-maven-plugin/11.7.2/flyway-maven-plugin-11.7.2.pom (3.8 kB at 4.9 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/flywaydb/flyway-parent/11.7.2/flyway-parent-11.7.2.pom Downloaded from central: https://repo.maven.apache.org/maven2/org/flywaydb/flyway-parent/11.7.2/flyway-parent-11.7.2.pom (36 kB at 6.5 kB/s) Downloading from central: https://repo.maven.apache.org/maven2/org/flywaydb/flyway-maven-plugin/11.7.2/flyway-maven-plugin-11.7.2.jar Progress (1): 33/110 kB这是什么意思
最新发布
09-02
在 Windows PowerShell 中执行 `mvn help:effective-settings` 命令后,输出内容包含了 Maven 实际生效的配置信息,其中警告信息和下载信息具有特定含义: #### 警告信息 - **配置文件相关警告** - **文件缺失或权限问题**:若提示 `settings.xml` 文件缺失或无法访问,可能是该文件路径错误、被删除或当前用户没有访问权限。例如,当 `settings.xml` 文件被意外删除,Maven 无法读取配置,就会给出此类警告。 - **配置格式错误**:当 `settings.xml` 文件中存在语法错误时,Maven 解析文件失败,会提示配置格式有误。比如 XML 标签未正确闭合,像 `<mirror>` 标签没有对应的 `</mirror>` 标签。 - **镜像配置警告** - **镜像地址不可用**:如果配置的镜像仓库地址无法访问,Maven 会发出警告。例如,镜像地址拼写错误,或者镜像服务器暂时不可用。 - **镜像配置冲突**:当多个镜像配置的 `mirrorOf` 规则冲突时,Maven 可能无法确定使用哪个镜像,从而产生警告。 - **插件相关警告** - **插件版本不兼容**:若使用的插件版本与当前 Maven 版本不兼容,可能会出现警告。例如,某些插件只支持特定范围的 Maven 版本,使用不匹配的版本就会触发警告。 - **插件缺失依赖**:插件在运行时可能依赖其他库,如果这些依赖缺失,Maven 会给出警告。 #### 下载信息 - **下载源信息**:输出中会显示 Maven 从哪些仓库下载依赖。例如,若配置了镜像,会显示从镜像仓库下载;若未配置镜像,可能会显示从中央仓库下载。 - **下载进度信息**:有时会显示依赖下载的进度,如已下载的字节数、总字节数和下载百分比等。 - **下载失败信息**:当依赖下载失败时,会显示失败的原因,如网络连接问题、仓库中不存在该依赖等。 ### 示例输出及分析 ```plaintext [WARNING] The POM for org.example:example-project:jar:1.0 is missing, no dependency information available ``` 此警告表明 `org.example:example-project:jar:1.0` 的 POM 文件缺失,Maven 无法获取该依赖的信息。可能是该依赖在仓库中不存在,或者网络问题导致无法下载 POM 文件。 ```plaintext Downloading from alimaven: https://maven.aliyun.com/repository/public/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.pom ``` 此信息显示 Maven 正在从阿里云镜像仓库(`alimaven`)下载 `maven-compiler-plugin` 的 POM 文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值