题解 | EXCEL排序

EXCEL排序

https://www.nowcoder.com/practice/bf3ec474bb7d410dbb9d5bbcd07a93e5

#include <iostream>
#include <algorithm>
using namespace std;
struct Node {
    string No;
    string Name;
    double score;
};
int m;
bool cmp(Node& a, Node& b) {
    if (m == 1) {
        return a.No < b.No;
    }
    else if (m == 2) {
        if (a.Name < b.Name)return true;
        if (a.Name == b.Name)return a.No < b.No;
        return false;
    }
    else {
        if (a.score < b.score)return true;
        if (a.score == b.score)return a.No < b.No;
        return false;
    }
}
int main() {
    int n;
    while (cin >> n) {
        if (n == 0)break;
        cin >> m;
        Node* nodes = new Node[n];
        for (int i = 0; i < n; ++i) {
            cin >> nodes[i].No >> nodes[i].Name >> nodes[i].score;
        }
        sort(nodes, nodes + n, cmp);
        cout << "Case:" << endl;
        for (int i = 0; i < n; ++i) {
            cout << nodes[i].No << " " << nodes[i].Name << " " << nodes[i].score << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-06 18:20
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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