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

UVaOJ足球赛排名系统
本篇博客介绍了一个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;
}


在使用 `git clone` 命令通过 HTTP 协议克隆 GitLab 仓库时,出现不推荐未加密 HTTP 且认证失败的错误信息,其含义具体如下: ### 不推荐未加密的 HTTP 使用未加密的 HTTP 协议传输数据存在安全风险,因为 HTTP 协议在数据传输过程中是明文的,这意味着传输的数据容易被中间人监听、篡改或窃取。例如,攻击者可以截取用户的用户名和密码等敏感信息。为了保障数据传输的安全性,GitLab 更推荐使用 HTTPS 协议,HTTPS 在 HTTP 的基础上加入了 SSL/TLS 加密层,能有效防止数据在传输过程中被窃取和篡改。 ### 认证失败 认证失败通常意味着 Git 客户端在尝试与 GitLab 服务器建立连接并验证身份时未能通过。可能的原因包括: - **用户名或密码错误**:用户输入的用户名或密码不正确,导致服务器无法验证用户身份。 - **权限问题**:用户可能没有足够的权限来访问该仓库。即使提供了正确的用户名和密码,如果用户没有被授权访问该仓库,服务器也会拒绝连接。 - **令牌问题**:如果使用访问令牌进行认证,令牌可能已过期、被撤销或格式不正确。 以下是一个示例错误信息及其含义: ```plaintext fatal: Unencrypted HTTP is not recommended for GitLab. Ensure the repository remote URL is using HTTPS or see https://aka.ms/gcm/unsaferemotes about how to allow unsafe remotes. Username for 'http://gitlab.example.com': Password for 'http://user@gitlab.example.com': remote: HTTP Basic: Access denied fatal: Authentication failed for 'http://gitlab.example.com/repo.git/' ``` 在这个示例中,首先提示不推荐使用未加密的 HTTP 协议,然后要求用户输入用户名和密码。最后,服务器返回 `HTTP Basic: Access denied` 错误,表明认证失败,可能是用户名、密码或权限问题导致的。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值