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$