解题过程的小记录,如有错误欢迎指出。
难度:一星(在做完1012后弄懂排序规则后此题并不难)
题目分析
给出若干个考点的考生的信息,求出给出考点排名和总排名,最后按照一定的格式进行输出。
注意点
- 两个并列第一名后是第三名
我的解题过程
思路
采用结构体存储考生信息,在输入的时候每个子循环结束后进行地方排名并赋值,输入完毕后进行整体排序赋值,最后按一定格式输出
bug
- 中途出现vector数组越界的问题,经检查后发现是因为在子循环中用了负循环的i作为下标所以越界,改成j后就正确了
- 因为有大量的输出所以在写的时候就害怕超时想用printf代替cout进行输出,代码如下
printf("%s %d %d %d\n", totaltestees[i].registration_number, totaltestees[i].final_rank, totaltestees[i].location_number, totaltestees[i].local_rank);
结果乱码了,查了资料原因了是因为printf是c语言的输出方法,然而string并不是c语言的内置类型所以这样输出会乱码,把C++中的string类型用c_str()转换一下格式即可正确输出,代码如下
printf("%s %d %d %d\n", totaltestees[i].registration_number.c_str(), totaltestees[i].final_rank, totaltestees[i].location_number, totaltestees[i].local_rank);
修改为printf输出后,提交显示的代码运行时间是原来的一半多一点
代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct testee {
string registration_number;
int score;
int location_number;
int local_rank;
int final_rank;
};
int compare(testee t1, testee t2) {
if (t1.score != t2.score) return t1.score > t2.score;
else return t1.registration_number < t2.registration_number;
}
int main()
{
int N, K;
cin >> N;
vector<testee> totaltestees;
//输入信息
for (int i = 0; i < N; i++) {
cin >> K;
vector<testee> localtestees;
for (int j = 0; j < K; j++) {
testee t;
cin >> t.registration_number >> t.score;
t.location_number = i + 1;
localtestees.push_back(t);
//totaltestees.push_back(t);******因为此时地方排名还没有完善所以还不能进行插入
}
sort(localtestees.begin(), localtestees.end(), compare);//每个子循环进行地方排名
localtestees[0].local_rank = 1;
totaltestees.push_back(localtestees[0]);
for (int j = 1; j < K; j++) {
localtestees[j].local_rank = j + 1;
if (localtestees[j].score == localtestees[j - 1].score) localtestees[j].local_rank = localtestees[j - 1].local_rank;
totaltestees.push_back(localtestees[j]);
}
}
sort(totaltestees.begin(), totaltestees.end(), compare);//总人数排名
totaltestees[0].final_rank = 1;
for (int i = 1; i < totaltestees.size(); i++) {
totaltestees[i].final_rank = i + 1;
if (totaltestees[i].score == totaltestees[i - 1].score) totaltestees[i].final_rank = totaltestees[i - 1].final_rank;
}
cout << totaltestees.size() << endl;
for (int i = 0; i < totaltestees.size(); i++) {
printf("%s %d %d %d\n", totaltestees[i].registration_number.c_str(), totaltestees[i].final_rank, totaltestees[i].location_number, totaltestees[i].local_rank);
//printf("%s\n", totaltestees[i].registration_number);
//cout << totaltestees[i].registration_number << ' ' << totaltestees[i].final_rank << ' ' << totaltestees[i].location_number << ' ' << totaltestees[i].local_rank << endl;
}
return 0;
}
dalao的代码
全部代码因版权原因不放出来,大家可以自行去柳神博客购买或者参考晴神的上机笔记~
借鉴点
- 如果想节省空间不写这么多vector数组的话,也可以采用对总的vector数组进行分段排序的方法,如下(参数无参考意义只是举例)
sort(totaltestees.begin() + i,totaltestees.begin() + i + K);
- 采用如下方式进行排名赋值更简洁
fin[j].finrank = (fin[j].score == fin[j - 1].score) ? (fin[j - 1].finrank) : (j + 1);
- 采用如下方式写比较函数更简洁
bool cmp1(student a, student b) {
return a.score != b.score ? a.score > b.score : a.no < b.no;
}