1085 PAT单位排行
题目:
每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。
输入格式:
输入第一行给出一个正整数 N(≤10^5 ),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:
准考证号 得分 学校
其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。
输出格式:
首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:
排名 学校 加权总分 考生人数
其中排名是该单位的排名(从 1 开始);学校是全部按小写字母输出的单位码;加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5的整数部分;考生人数是该属于单位的考生的总人数。
学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。
输入样例:
10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu
输出样例:
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
代码:
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#include<math.h>
using namespace std;
struct school {
string school_name;
double school_score;
//double score;
int num;
};
bool cmp(struct school A, struct school B) {
if (int(A.school_score) != int(B.school_score))
return A.school_score > B.school_score;
else if (A.num != B.num)
return A.num < B.num;
else
return A.school_name < B.school_name;
//这俩种比较方法都是正确的
/*if (A.school_score > B.school_score)
return 1;
else if (A.school_score == B.school_score) {
if (A.num < B.num)
return 1;
else if (A.num == B.num) {
if (A.school_name < B.school_name)
return 1;
}
}
return 0;*/
}
int main() {
int N;
cin >> N;
vector<school> p;
map<string,int> index;
int k = 0;
for (int i = 0; i < N; i++) {
string id;
double score;//注意这里也要是double
string school_name;
cin >> id >> score >> school_name;
for (int k = 0; k < school_name.length(); k++) {//统一转为小写
if (school_name[k] >= 'A'&&school_name[k] <= 'Z')
school_name[k] = school_name[k] + ('a' - 'A');
}
if (index[school_name] != 0) {
p[index[school_name]-1].num += 1;
if (id[0] == 'B')
p[index[school_name]-1].school_score += 1.0*score / 1.5;
else if(id[0] == 'A')
p[index[school_name]-1].school_score += 1.0*score;
else if (id[0] == 'T')
p[index[school_name]-1].school_score += 1.0*score*1.5;
}
else {
index[school_name] = k + 1;
k++;
if (id[0] == 'B')
p.push_back(school{ school_name ,score / 1.5, 1 });
else if (id[0] == 'A')
p.push_back(school{ school_name,score, 1 });
else if (id[0] == 'T')
p.push_back(school{ school_name,score*1.5, 1 });
}
}
for (int i = 0; i < p.size(); i++) {
p[i].school_score =int(p[i].school_score);
}
cout << p.size() << endl;
sort(p.begin(), p.end(), cmp);
int tim = 1;
for (int i = 0; i < p.size(); i++) {
if (i == 0) {
cout << tim << " " << p[i].school_name << " ";
printf("%d", int(p[i].school_score));
cout << " " << p[i].num << endl;
}
else {
if (p[i].school_score != p[i - 1].school_score)
{
tim = i + 1;
cout << tim << " " << p[i].school_name << " ";
printf("%d", int(p[i].school_score));
cout << " " << p[i].num << endl;
}
else {
cout << tim << " " << p[i].school_name << " ";
printf("%d", int(p[i].school_score));
cout << " " << p[i].num << endl;
}
}
//注意下面这种写法:会使测试点4、5答案错误,要尽可能比避免浮点数的比较
/*if (i != 0 && p[i].school_score ==p[i - 1].school_score )
{
cout << i << " " << p[i].school_name << " ";
printf("%d", int(p[i].school_score));
cout << " " << p[i].num << endl;
}
else {
cout << i+1<< " " << p[i].school_name << " ";
printf("%d", int(p[i].school_score));
cout << " " << p[i].num << endl;
}*/
}
}
/*
测试点4、5其实就是题目组中重点加粗部分的理解
这个是每个学生加起来以后再取整,而不是每次都取整!
解决了这个以后就变得简单了。
*/
注意:
-
一开始测试点4、5一直过不去
-
测试点4、5其实就是题目组中重点加粗部分的理解
bash 加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5的整数部分;
这个是每个学生加起来以后再取整,而不是每次都取整!
-
还要注意就是在输出的时候:
//注意下面这种写法:会使测试点4、5答案错误,要尽可能比避免浮点数的比较
/*if (i != 0 && p[i].school_score ==p[i - 1].school_score )
{
cout << i << " " << p[i].school_name << " ";
printf("%d", int(p[i].school_score));
cout << " " << p[i].num << endl;
}
else {
cout << i+1<< " " << p[i].school_name << " ";
printf("%d", int(p[i].school_score));
cout << " " << p[i].num << endl;
}*/
printf("%d", int(p[i].school_score));