P1093 [NOIP2007 普及组] 奖学金 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
这道题和上道题很像:P1781 宇宙总统 - 洛谷 (大数,字符串比较) - 掘金 (juejin.cn)
同样用一个结构体存储每个学生的信息,结构体包括:语数英成绩,学号
再写个cmp函数按照题目的要求进行比较即可。
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct student
{
int Chinesecj;
int Matchcj;
int Englishcj;
int xh;
};
// 比较函数,用于排序
bool cmp(student a, student b)
{
if (a.Chinesecj + a.Matchcj + a.Englishcj != b.Chinesecj + b.Matchcj + b.Englishcj)
return a.Chinesecj + a.Matchcj + a.Englishcj > b.Chinesecj + b.Matchcj + b.Englishcj;
if (a.Chinesecj != b.Chinesecj)
return a.Chinesecj > b.Chinesecj;
if (a.xh != b.xh)
return a.xh < b.xh;
return false;
}
int main()
{
int n;
cin >> n;
student st[n];
for (int i = 0; i < n; i++)
{
cin >> st[i].Chinesecj >> st[i].Matchcj >> st[i].Englishcj;
st[i].xh = i + 1; // 学号从1开始
}
// 排序学生信息
sort(st, st + n, cmp);
for(int i=0;i<5;i++)
{
cout<<st[i].xh<<" "<<st[i].Chinesecj +st[i].Matchcj + st[i].Englishcj <<endl;
}
// 找到总分最高的学生
// student maxn = st[0];
// for (int i = 1; i < n; i++)
// {
// if (st[i].Chinesecj + st[i].Matchcj + st[i].Englishcj > maxn.Chinesecj + maxn.Matchcj + maxn.Englishcj)
// {
// maxn = st[i];
// }
// }
//
// cout << maxn.xh << endl << maxn.Chinesecj + maxn.Matchcj + maxn.Englishcj << endl;
return 0;
}