【结构体排序】HDU-1225 Football Score

在这里插入图片描述
在这里插入图片描述

注解

1、结构体排序。要存储的是队名,积分,净胜球数,进球数。
2、用map记录球队数,判断每次读入的队名是否是新增的队名。

代码

#include <iostream>
#include <algorithm>
#include <map>

using namespace std;

struct Team {
    string name;
    int score;
    int winGoal;
    int goal;
};

int compare(Team t1, Team t2) {
    if(t1.score!=t2.score) {
        return t1.score>t2.score;
    }
    if(t1.winGoal!=t2.winGoal) {
        return t1.winGoal>t2.winGoal;
    }
    if(t1.goal!=t2.goal) {
        return t1.goal>t2.goal;
    }
    return t1.name<t2.name;
}

int main() {
    
    int N;
    while(cin>>N) {
        
        Team t[N+1];
        for(int i=1; i<=N; i++) {
            t[i].name = "";
            t[i].score = 0;
            t[i].winGoal = 0;
            t[i].goal = 0;
        }
        
        map<string, int> m;
        int id = 0;

        for(int i=0; i<N*(N-1); i++) {
            string name1;
            string vs;
            string name2;
            int goal1;
            char c;
            int goal2;

            cin>>name1>>vs>>name2>>goal1>>c>>goal2;

            int pos1 = m[name1];
            if(pos1==0){
                m[name1] = ++id;
                t[id].name = name1;
                pos1 = id;
            }
            int pos2 = m[name2];
            if(pos2==0){
                m[name2] = ++id;
                t[id].name = name2;
                pos2 = id;
            }
            if(goal1>goal2) {
                t[pos1].score += 3;
            } else if(goal1==goal2) {
                t[pos1].score += 1;
                t[pos2].score += 1;
            } else {
                t[pos2].score += 3;
            }
            t[pos1].winGoal += goal1-goal2;
            t[pos2].winGoal += goal2-goal1;
            t[pos1].goal += goal1;
            t[pos2].goal += goal2;
        }

        sort(t+1, t+1+N, compare);

        for(int i=1; i<=N; i++) {
            cout<<t[i].name<<" "<<t[i].score<<endl;
        }
        cout<<endl;
    }

    return 0;
}

结果

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值