题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805260353126400
坑点:mp.size() 的值为2倍的映射数量,一开始v的尺寸初始化错误了
还有就是用来整体大小写转化transform(temp_school.begin(), temp_school.end(), temp_school.begin(), ::tolower);
函数将对从输入参数的first1-last1的全部变量做op函数操作。结果保存到result中,或是通过返回值返回。
代码
#include<string>#include<iostream>#include<map>#include<vector>#include<cstdio>#include<algorithm>using namespace std;struct School{int rank, nums, total2;double total1;string name;};double Cal(char a, double b){if(a == 'B') return b / 1.5;else if(a == 'A') return b;else if(a == 'T') return b * 1.5;}bool cmp(School a, School b){if(a.total2 != b.total2) return a.total2 > b.total2;else if(a.nums != b.nums) return a.nums < b.nums;else return a.name < b.name;}int main(){int n;string temp_id, temp_school;double temp_score;map<string, School > mp;scanf("%d", &n);for(int i = 0; i < n; i++){cin >> temp_id >> temp_score >> temp_school;transform(temp_school.begin(), temp_school.end(), temp_school.begin(), ::tolower);if(mp.find(temp_school) == mp.end()){mp[temp_school].name = temp_school;mp[temp_school].nums = 1;mp[temp_school].total1 = Cal(temp_id[0], temp_score);} else {mp[temp_school].nums++;mp[temp_school].total1 += Cal(temp_id[0], temp_score);}}vector<School> v;for(auto it = mp.begin(); it != mp.end(); it++){it->second.total2 = (int)it->second.total1;v.push_back(it->second);}sort(v.begin(), v.end(), cmp);cout << v.size() << endl;v[0].rank = 1;for(int i = 1; i < v.size(); i++){if(v[i].total2 == v[i - 1].total2) v[i].rank = v[i - 1].rank;else v[i].rank = i + 1;}for(int i = 0; i < v.size(); i++){cout << v[i].rank <<" "<< v[i].name <<" "<< v[i].total2 <<" "<< v[i].nums << endl;}return 0;}
