#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<unordered_map>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;
unordered_map<string, int> sNum;
unordered_map<string, int> sScore;
const int maxv = 1e5 + 5;
int n;
struct node{
string school;
int scoreT, num;
};
bool cmp(node a, node b){
if(a.scoreT != b.scoreT) return a.scoreT > b.scoreT;
else if(a.num != b.num) return a.num < b.num;
else return a.school < b.school;
}
int main(){
scanf("%d",&n);
string id,schoolName;
for(int i = 0; i < n; i++){
double score;
cin>>id;
scanf("%lf",&score);
cin>>schoolName;
for(int i = 0; i< schoolName.length();i++){
if(schoolName[i] >= 'A' && schoolName[i] <= 'Z'){
schoolName[i] = tolower(schoolName[i]);//学习名称变小写
}
}
if(id[0] == 'B'){
score = score / 1.5;
}else if(id[0] == 'T'){
score = score * 1.5;
}
sNum[schoolName]++;
sScore[schoolName] += score;
}
vector<node> ans;
//unordered_map<string, int> ::iterator it;
//用auto 就不要写上面的这一行了
for(auto it = sNum.begin(); it != sNum.end();it++){
// node temp = new node;
// temp.school = it->first;
// temp.scoreT = sScore[it->first];
// temp.num = sNum[it->first];
//简化为以下一句:
ans.push_back(node{it->first,(int)sScore[it->first],sNum[it->first] });
}
sort(ans.begin(), ans.end(),cmp);
printf("%d\n", (int)ans.size());//学校个数
int rank = 0, now = -1;//排名
for(int i = 0; i < ans.size(); i++){
if(ans[i].scoreT != now) rank = i+1;
now = ans[i].scoreT;
printf("%d ", rank);
cout<<ans[i].school;
printf(" %d %d\n", ans[i].scoreT, ans[i].num);
}
return 0;
}
不熟悉的地方:
1.unordered_map的使用:
要加头文件 #include<unordered_map>
其实可以理解为 下标 可以为字符串的 数组
unordered_map<string, int> sNum;
unordered_map<string, int> sScore;
这样sNum[name] ++;实现计数功能
sScore[name]实现 统计分数
而用 unordered_map 会比map快一点
当超时的时候可以考虑换成这个 出现调用头文件错误的时候 ; 解决方法 https://blog.csdn.net/liu_feng_zi_/article/details/93714023
2. 大小写转换
cin>>schoolName;
for(int i = 0; i< schoolName.length();i++){
if(schoolName[i] >= 'A' && schoolName[i] <= 'Z'){
schoolName[i] = tolower(schoolName[i]);//学习名称变小写
}
}
tolower();
toupper();
要加头文件 #include<cctype>
//
3. double 的输入用%1f
是1 (1233 的 1)
不是l !!!