wjklprqy @ 2022-11-10 17:15:00
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 310;
int n;
struct student{
int name;
double chinese;
double score;
}stu[N];
bool cmp(student a, student b)
{
if (a.score != b.score) return a.score > b.score;
else
{
if (a.chinese != a.chinese) return a.chinese > b.chinese;
else return a.name < b.name;
}
}
int main()
{
double a, b, c;
cin >> n;
for (int i = 1; i <= n; i ++ )
{
cin >> a >> b >> c;
stu[i].name = i;
stu[i].chinese = a;
stu[i].score = a + b + c;
}
sort(stu + 1, stu + n + 1, cmp);
for (int i = 1; i <= 5; i ++ )
{
cout << stu[i].name << " " << stu[i].score << endl;
}
return 0;
}
by jiangjintao009 @ 2022-11-10 17:53:27
那道
by Castaways @ 2022-12-29 19:35:29
cmp函数没必要写的这么复杂,可以在结构体里面重载大于号。我照着你的代码改了一下。
Code
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 310;
int n;
struct student{
int name;
double chinese;
double score;
bool operator > (const student &b) const{
if(score!=b.score) return score>b.score;
if(chinese!=b.chinese) return chinese>b.chinese;
return name<b.name;
}
}stu[N];
//bool cmp(student a, student b)
//{
// if (a.score != b.score) return a.score > b.score;
// else
// {
// if (a.chinese != a.chinese) return a.chinese > b.chinese;
// else return a.name < b.name;
// }
//}
bool cmp(student x,student y){return x>y;}
int main()
{
double a, b, c;
cin >> n;
for (int i = 1; i <= n; i ++ )
{
cin >> a >> b >> c;
stu[i].name = i;
stu[i].chinese = a;
stu[i].score = a + b + c;
}
sort(stu + 1, stu + n + 1, cmp);
for (int i = 1; i <= 5; i ++ )
{
cout << stu[i].name << " " << stu[i].score << endl;
}
return 0;
}