重载运算符版题解
2025-08-27 18:07:43
发布于:上海
28阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
struct player {
    char s[25];
    int a, b, c, sum = 0, id;
    bool operator < (player &opponent) {
        if (sum != opponent.sum) return sum > opponent.sum;
        if (a != opponent.a) return a > opponent.a;
        if (b != opponent.b) return b > opponent.b;
        return id < opponent.id;
    }
    friend istream& operator >> (istream& in, player& P) {
        in >> P.s >> P.a >> P.b >> P.c;
        P.sum = P.a + P.b + P.c;
        return in;
    }
} p[100005];
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        p[i].id = i;
    } sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; i++) printf("%s %d\n",p[i].s, p[i].sum);
    return 0;
}
整体就输入后排序再输出即可,注意到结构体内不能写 string 否则会出现乱码。
全部评论 1
为什么string会乱码
2025-10-14 来自 上海
0我怎么知道
2025-10-15 来自 上海
0












有帮助,赞一个