hereweare1 @ 2023-07-06 17:09:38
结构体+sort 按顺序依次排序
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
class student
{
public:
int Chinese;
int Math;
int English;
int score;
int ID;
};
bool score_Sort(student s1, student s2)
{
return s1.score > s2.score?1:s1.score==s2.score?s1.ID<s2.ID:0;
}
bool Chinese_Sort(student s1, student s2)
{
return s1.Chinese > s2.Chinese;
}
int main()
{
vector<student> Stu;
int n;
cin >> n;
Stu.resize(n);
for (int i = 0; i < n; i++)
{
cin >> Stu[i].Chinese >> Stu[i].Math >> Stu[i].English;
Stu[i].score= Stu[i].Chinese +Stu[i].Math + Stu[i].English;
Stu[i].ID = i + 1;
}
sort(Stu.begin(), Stu.end(), Chinese_Sort);
sort(Stu.begin(), Stu.end(), score_Sort);
for (int i = 0; i < 5; i++)
{
cout << Stu[i].ID << " " << Stu[i].score << endl;
}
}
by Rolmcy @ 2023-08-03 13:55:07
我懒不想看,之前写的我也不想看,你参考下吧
#include <bits/stdc++.h>
using namespace std;
struct node {
int a,b,c,d,w;//w为学号,a为语文,d为总分
};
bool f(node a,node b) {
if(a.d<b.d) {
return 0;
} else if(a.d==b.d&&a.a<b.a) {
return 0;
} else if(a.d==b.d&&a.a<b.a&&a.w>b.w) {
return 0;
}
return 1 ;
}
int main() {
node r[305];
int n;
cin>>n;
for(int i=1; i<=n; i++) {
cin>>r[i].a>>r[i].b>>r[i].c;
r[i].d=r[i].a+r[i].b+r[i].c;
r[i].w=i;
}
sort (r+1,r+n+1,f);
for (int i=1; i<=5; i++) {
cout <<r[i].w<<" "<<r[i].d<<endl;
}
return 0;
}