题解 | #成绩排序#

成绩排序

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")

全部评论

相关推荐

青春运维少年不会梦到...:实习大王
点赞 评论 收藏
分享
09-29 00:03
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务