题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
此题可以通过桶排序解决。设定vector<vector<string>> v(101)
将输入的成绩转换为int类型score,并将学生名字作为string变量push_back进v[score]
最后按要求顺序输出v即可
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n, signal;
cin >> n >> signal;
vector<vector<string>> v(101);
vector<string> input;
string temp;
while (getline(cin,temp)) {
if(temp.empty()){
continue;
}else {
input.push_back(temp);
}
}
for (auto& j : input) {
vector<string> tmp(2);
istringstream s(j);
for (int i = 0; getline(s, tmp[i], ' '); i++);
int score = stoi(tmp[1]);
v[score].push_back(tmp[0]);
}
if (signal == 0) { //从高到低
for (int i = 100; i >= 0; i--) {
if (!v[i].empty()) {
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << ' ' << i << endl;
}
}
}
} else {
for (int i = 0; i <= 100; i++) {
if (!v[i].empty()) {
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << ' ' << i << endl;
}
}
}
}
}
// 64 位输出请用 printf("%lld")
查看6道真题和解析