一个专属于Clion用户的题解
2024-12-02 12:04:14
发布于:黑龙江
结构体这个东西感觉是真的没啥用,完全可以被类取代
代码:
#include <bits/stdc++.h>
using namespace std;
class Stu {
public:
string name;
int id;
int chi;
int mat;
int eng;
int all;
};
istream& operator>>(istream& is, Stu& stu) {
is >> stu.name >> stu.chi >> stu.mat >> stu.eng;
stu.all = stu.chi + stu.mat + stu.eng;
return is;
}
bool cmp(const Stu &a, const Stu &b) {
if (a.all != b.all) return a.all > b.all;
else {
if (a.chi != b.chi) return a.chi > b.chi;
else {
if (a.mat != b.mat) return a.mat > b.mat;
else return a.id < b.id;
}
}
}
int main() {
int n;
cin >> n;
Stu arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
arr[i].id=i;
}
sort(arr, arr+n, cmp);
for (int i = 0; i < n; i++) {
cout << arr[i].name << ' ' << arr[i].all << endl;
}
}
有人可能问了"你这又是重载又是类的,写起来多浪费时间啊",我们Clion是这样的,打两个字母一行都出来了,完全不会浪费时间
这里空空如也
有帮助,赞一个