题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37
/*
思路:
1. 创建一个 struct student 结构体,里面包含学生 成绩 与 分数。
2. 再创建一个数组 vector<struct student>, 把读到的学生放到数组中
3. 使用 stable_sort 对数据进行排序
4. 自定以 stable_sort 的排序函数
5. 打印输出结果。
*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 自定义比较函数
struct user{
string name;
int score;
};
bool myIncrease(struct user stTmpUser1, struct user stTmpUser2){
return stTmpUser1.score < stTmpUser2.score;
}
bool myDecrease(struct user stTmpUser1, struct user stTmpUser2){
return stTmpUser1.score > stTmpUser2.score;
}
int main() {
int n, bisIncrease;
cin >> n >> bisIncrease;
vector<struct user> userForm;
struct user stTmpUser;
for(int i = 0; i < n; i++){
cin >> stTmpUser.name >> stTmpUser.score;
userForm.push_back(stTmpUser);
}
if(bisIncrease){
stable_sort(userForm.begin(), userForm.end(), myIncrease);
}
else{
stable_sort(userForm.begin(), userForm.end(), myDecrease );
}
for(int i = 0; i < n; i++){
cout << userForm[i].name << " " << userForm[i].score << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
字符串操作 文章被收录于专栏
字符串操作

上海得物信息集团有限公司公司福利 1240人发布