2021-03-31-华为实习-机试题


CSDN第一题:2021-03-31华为
牛客:三题

题目一

1. 描述

题目:双循环赛制每两只球队都会各比一次,赢了积3分,平手各积1分,输了不积分。现最多有26支队伍,用a,b,c…表示,统计所有球队的积分并按照从高到低排序,如果两支队伍的积分相同,则按球队的字典序排序。队伍之间用“,”分隔,输入以空行结束。

示例:

测试用例1:
输入:
a-b 3:0
b-a 1-1
输出:
a 4,b 1
解释:a球队胜利积3分,a与b平各积1分,a得4分b得1分

测试用例2:
输入:
a-b 3:0
a-c 2:1
b-a 1:1
c-a 0:1
b-c 4:3
c-b 2:2
输出:
a 10,b 5,c 1

2. 代码

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <queue>

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>

using namespace std;


int str2num(string s)
{   
    int num;
    stringstream ss(s);
    ss>>num;
    return num;
}

int str2char(string s)
{   
    char ch;
    stringstream ss(s);
    ss>>ch;
    return ch;
}

struct Team{
    char name;
    unsigned int score;
    bool occur;
};

struct cmp{
    bool operator ()(Team a, Team b){
        if(a.score > b.score){
            return false;
        }
        else{
            return a.name > b.name;
        }  
    }
};

class Solution{
public:
    Team team[26];
public: 
    void getScore(){
        for(int i = 0; i < 26; i++){
            team[i].name = char(97 + i);
            team[i].score = 0;
            team[i].occur = false;
        }

        string teams_name;
        string teams_score;

        int count = 0;

        while(cin >> teams_name >> teams_score){
            vector<string> v_str_name;
            vector<string> v_str_score;
            boost::split(v_str_name, teams_name, boost::is_any_of("-"), boost::token_compress_on);
            boost::split(v_str_score, teams_score, boost::is_any_of(":"), boost::token_compress_on);

            if(str2num(v_str_score[0]) > str2num(v_str_score[1])){
                team[str2char(v_str_name[0]) - 97].score += 3;
                team[str2char(v_str_name[0]) - 97].occur = true;
            }
            else if(str2num(v_str_score[0]) == str2num(v_str_score[1])){
                team[str2char(v_str_name[0]) - 97].score += 1;
                team[str2char(v_str_name[0]) - 97].occur = true;
                team[str2char(v_str_name[1]) - 97].score += 1;
                team[str2char(v_str_name[1]) - 97].occur = true;
            }
            else{
               team[str2char(v_str_name[1]) - 97].score += 3; 
               team[str2char(v_str_name[1]) - 97].occur = true;
            }

            count++;
            if(count > 5) break;
        } 

        priority_queue<Team,vector<Team>,cmp> Q;
        
        for(int i = 0; i < 26; i++){
            if(team[i].occur){
                Q.push(team[i]);
            }
        }

        while(!Q.empty()){
            cout << Q.top().name << " " << Q.top().score << ",";
            Q.pop();
        }
    }   
};


int main(int argc, char const *argv[])
{
    Solution sol;
    sol.getScore();
    return 0;
}

运行结果:

seivl@seivl-Default-string:~/for_code/for_code/build$ ./for_code 
a-b 3:0
a-c 2:1
b-a 1:1
c-a 0:1
b-c 4:3
c-b 2:2
a 10,b 5,c 1,seivl@seivl-Default-string:~/for_code/for_code/build$
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值