3.14牛客2021年度训练联盟热身训练赛第二场D.Soccer Standings[模拟and重载运算符and用map导航struct中的string]

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述 

Soccer fever has gripped the world once again, and millions of people from dozens of countries 

will be glued to their TV sets for the World Cup.  Being an enterprising sort, you’ve started up your own internet World Cup Soccer Channel for streaming matches online.  Recently you came up  with  the  idea  of  filling  up  the  time  between  matches  by  having  a  couple  of  ‘experts’  offer critical analysis of games.  For this purpose, you have devised a unique ranking system for soccer teams, which you must now implement.
 The Problem:
Given  a  list  of  teams  and  a  list  of  match  scores,  you  must  compute  several  quantities  for  each team.  These are: the total number of goals scored over all their games, the total number of goals scored  against  them (goals  allowed,  for  short),  the  number  of  wins,  draws  and  losses,  and  the number  of  points  scored  so  far.  Points  are  to  be  computed  as  follows:  winning  a  match  nets  a team 3 points, losing gets them nothing.  In the event of a tie, both teams get 1 point.
 In addition to this, you must order the teams correctly according to your new system.  Teams are ordered according to points, from highest to lowest.  In the event of a tie in points, the team that has a  higher  goal  difference comes  first.  The  goal  difference  is  defined  as  the  total  number  of goals scored by the team minus the total number of goals scored against them.
  If there is still a tie (i.e., two or more teams have the same points and the same goal differences), the  team  with  higher  total  goals  scored  comes  first.   If  even  this  is  tied,  the  team  whose  name comes first in alphabetical order goes first. 

 

输入描述:

 

The  first  input  line  contains  a  positive integer,  n,  indicating  the  number  of  data  sets  to  be processed.  The first line of each data set consists of two positive integers T (T ≤ 30) and G (G ≤ 
400)  –  the  number  of  teams  in  this  group  and  the  total  number  of  games  played  by  them.  The next line contains T unique names separated by single spaces.  Each name is a single uppercase word with no more than 15 characters.
 Each  of  the  next  G  input  lines  will  contain  the  results  of  a  match.  Each  line  is  of  the  form
 <country_1>  <score_1>  <country_2>  <score_2>.  For  example,  “Greece  2  Nigeria  1” indicates that Greece and Nigeria played a game with score 2-1.  All four terms will be separated by single spaces.

 

输出描述:

 

At  the  beginning  of  output  for  each  data  set,  output  “Group  g:”  where  g  is  the  data  set number,  starting  from  1.   Next  you  should  print  a  single  line  for  each  team,  ordering  teams  as mentioned  above.  For  each  team,  the line  you  print  should  be  of  the  form  “<name>  <points> <wins>  <losses>  <draws>  <goals  scored>  <goals  allowed>”.   These  items  should  be separated by single spaces. Leave a blank line after the output for each data set. 

 

示例1

输入

复制

2
2 1
KASNIA LATVERIA
KASNIA 0 LATVERIA 1
4 6
ENGLAND USA ALGERIA SLOVENIA
ENGLAND 1 USA 1
ALGERIA 0 SLOVENIA 1
SLOVENIA 2 USA 2
ENGLAND 0 ALGERIA 0
SLOVENIA 0 ENGLAND 1
USA 1 ALGERIA 0

输出

复制

Group 1:
LATVERIA 3 1 0 0 1 0
KASNIA 0 0 1 0 0 1

Group 2:
USA 5 1 0 2 4 3
ENGLAND 5 1 0 2 2 1
SLOVENIA 4 1 1 1 3 3
ALGERIA 1 0 2 1 0 2

备注:

简直了,我被这道题卡了2个半小时直接卡死了,最后那道感觉我可以做的背包题都没做完

X的,真的不懂什么足球规则还是英文的,我太难了

问题在于

1.struct里定义的diff值只会是0,diff的value在f里使用时再计算出来

bool f(struct team a,struct team b)
{
    int agd=a.goals_scored-a.goals_allowed;
    int bgd=b.goals_scored-b.goals_allowed;
    if(a.points!=b.points)
    {
        return a.points>b.points;
    }
    else if(agd!=bgd)
    {
        return agd>bgd;
    }
    else if(a.goals_scored!=b.goals_scored)
    {
        return a.goals_scored>b.goals_scored;
    }
    else if(a.name!=b.name)
    {
        return a.name<b.name;
    }
    return false;
}
2.重载的f里name按字典序较前的应该返回的是a.name<b.name;

3.多重输入时team初始化定义在循环内

struct Stu{

    string s;

    int points,wins,losses,draws,g1,g2,g3;

    void clear(){

        points=wins=losses=draws=g1=g2=g3=0;

    }

}stu[405];

也可以像榜一一样在struct里定义一个clear函数,在进入循环时调用

stu[i].clear();

4.goals_allowed原来就是对手的进球。。。。。。

            t[mp[a]].goals_scored+=as;
            t[mp[a]].goals_allowed+=bs;
            t[mp[b]].goals_scored+=bs;
            t[mp[b]].goals_allowed+=as;

5。看榜一代码我意识到了一个问题,我map在使用时忘记清空了,幸亏这道题没有更改缩写的含义

map<string,int>mp;

mp.clear();

❀我觉得我用map来导航struct十分的精妙(是我自己想到的,榜一也是这样做的😁)

map<string,int>mp;

for(int i=0;i<Tm;i++)
        {
            cin>>t[i].name;
            mp[t[i].name]=i;
        }

t[mp[a]].goals_allowed+=bs;
t[mp[b]].goals_scored+=bs;

a,b是string,可以直接修改string对应struct中的值

wa1

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
#define Inf 0xfffffff
#define N 501000
#define eps 1e-7
using namespace std;
 
struct team
{
    string name;
    int points=0;
    int wins=0;
    int losses=0;
    int draws=0;
    int goals_scored=0;
    int goals_allowed=0;
    int goals_diff=goals_scored-goals_allowed;
    
};

map<string,int>mp;

bool f(struct team a,struct team b)
{
    if(a.points!=b.points)
    {
        return a.points>b.points;
    }
    else if(a.goals_diff!=b.goals_diff)
    {
        return a.goals_diff>b.goals_diff;
    }
    else if(a.goals_scored!=b.goals_scored)
    {
        return a.goals_scored>b.goals_scored;
    }
    else if(a.name!=b.name)
    {
        return a.name>b.name;
    }
    return true;
}

int main()
{
    int n;
    cin>>n;
    int gr=1;
    while(gr<=n)
    {
        int Tm,G;
        cin>>Tm>>G;
        team t[40];
        for(int i=0;i<Tm;i++)
        {
            cin>>t[i].name;
            mp[t[i].name]=i;
        }
        //getchar();
        for(int i=0;i<G;i++)
        {
            string a,b;
            int as,bs;
            cin>>a>>as>>b>>bs;
            if(as>bs)
            {
                t[mp[a]].points+=3;
                t[mp[a]].wins+=1;
                t[mp[b]].losses+=1;
                t[mp[b]].goals_allowed+=as;
                t[mp[a]].goals_scored+=as;
            }
            else if(as==bs)
            {
                t[mp[a]].points+=1;
                t[mp[b]].points+=1;
                t[mp[a]].draws+=1;
                t[mp[b]].draws+=1;
                t[mp[a]].goals_allowed+=as;
                t[mp[b]].goals_allowed+=bs;
                t[mp[a]].goals_scored+=as;
                t[mp[b]].goals_scored+=bs;
            }
            else if(as<bs)
            {
                t[mp[b]].points+=3;
                t[mp[b]].wins+=1;
                t[mp[a]].losses+=1;
                t[mp[a]].goals_allowed+=bs;
                t[mp[b]].goals_scored+=bs;
            }
        }
        sort(t,t+Tm,f);
        cout<<"Group "<<gr<<":"<<endl;
        for(int i=0;i<Tm;i++)
        {
            cout<<t[i].name<<" "<<t[i].points<<" "<<t[i].wins<<" "<<t[i].losses<<" "<<t[i].draws<<" "<<t[i].goals_scored<<" "<<t[i].goals_allowed<<endl;
        }
        if(gr!=n)
            cout<<endl;
        gr++;
    }
    return 0;
}

ac

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
#define Inf 0xfffffff
#define N 501000
#define eps 1e-7
using namespace std;
 
struct team
{
    string name;
    int points=0;
    int wins=0;
    int losses=0;
    int draws=0;
    int goals_scored=0;
    int goals_allowed=0;
    
};

map<string,int>mp;

bool f(struct team a,struct team b)
{
    int agd=a.goals_scored-a.goals_allowed;
    int bgd=b.goals_scored-b.goals_allowed;
    if(a.points!=b.points)
    {
        return a.points>b.points;
    }
    else if(agd!=bgd)
    {
        return agd>bgd;
    }
    else if(a.goals_scored!=b.goals_scored)
    {
        return a.goals_scored>b.goals_scored;
    }
    else if(a.name!=b.name)
    {
        return a.name<b.name;
    }
    return false;
}

int main()
{
    int n;
    cin>>n;
    int gr=1;
    while(gr<=n)
    {
        int Tm,G;
        cin>>Tm>>G;
        team t[40];
        for(int i=0;i<Tm;i++)
        {
            cin>>t[i].name;
            mp[t[i].name]=i;
        }
        //getchar();
        for(int i=0;i<G;i++)
        {
            string a,b;
            int as,bs;
            cin>>a>>as>>b>>bs;
            t[mp[a]].goals_scored+=as;
            t[mp[a]].goals_allowed+=bs;
            t[mp[b]].goals_scored+=bs;
            t[mp[b]].goals_allowed+=as;
            if(as>bs)
            {
                t[mp[a]].points+=3;
                t[mp[a]].wins+=1;
                t[mp[b]].losses+=1;
            }
            else if(as==bs)
            {
                t[mp[a]].points+=1;
                t[mp[b]].points+=1;
                t[mp[a]].draws+=1;
                t[mp[b]].draws+=1;
            }
            else if(as<bs)
            {
                t[mp[b]].points+=3;
                t[mp[b]].wins+=1;
                t[mp[a]].losses+=1;
            }
        }
        sort(t,t+Tm,f);
        cout<<"Group "<<gr<<":"<<endl;
        for(int i=0;i<Tm;i++)
        {
            cout<<t[i].name<<" "<<t[i].points<<" "<<t[i].wins<<" "<<t[i].losses<<" "<<t[i].draws<<" "<<t[i].goals_scored<<" "<<t[i].goals_allowed<<" "<<endl;
        }
        if(gr!=n)
            cout<<endl;
        gr++;
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值