这道题如果在每一次读取的时候更新分数就把它强制转换成int,测试点5会答案错误
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
struct School{
string name;
double score;
int finalScore;
int testees;
School(string n){
name = n;
score = 0;
finalScore = 0;
testees = 1;
}
School(){}
};
char op(char ch){
return (ch >= 'A' && ch <= 'Z') ? ch + 'a' - 'A' : ch;
}
string toLowerCase(string s){
int len = s.length();
for(int i = 0;i < len;i++)
s[i] = op(s[i]);
return s;
}
unordered_map<string,int> strToIndex; // 学校名在数组中对应的编号
vector<School> schools;
bool cmp(School a,School b){
if(a.finalScore != b.finalScore)
return a.finalScore > b.finalScore;
else if(a.testees != b.testees)
return a.testees < b.testees;
else
return a.name < b.name;
}
int main(){
int n;
scanf("%d",&n);
for(int i = 0;i < n;i++){
string id,school;
int score;
cin>>id>>score>>school;
school = toLowerCase(school);
int index;
if(strToIndex.find(school) == strToIndex.end()){
schools.push_back(School(school));
index = schools.size() - 1;
strToIndex[school] = index;
}else{
index = strToIndex[school];
schools[index].testees++;
}
if(id[0] == 'T')
schools[index].score += 1.5 * score;
else if(id[0] == 'A')
schools[index].score += score;
else
schools[index].score += score / 1.5;
schools[index].finalScore = schools[index].score;
}
sort(schools.begin(),schools.end(),cmp);
int lastrank = 1;
printf("%d\n",schools.size());
printf("1 %s %d %d\n",schools[0].name.c_str(),schools[0].finalScore,schools[0].testees);
for(int i = 1;i < schools.size();i++){
if(schools[i].finalScore == schools[i - 1].finalScore)
printf("%d %s %d %d\n",lastrank,schools[i].name.c_str(),schools[i].finalScore,schools[i].testees);
else{
printf("%d %s %d %d\n",i + 1,schools[i].name.c_str(),schools[i].finalScore,schools[i].testees);
lastrank = i + 1;
}
}
system("pause");
return 0;
}