F题:奖学金
思路:
1、对所有学生按照给定的优先级进行排序,即先看总分,(若总分相同)再看语文分数,(若语文也相同)再看学号。
2、快速排序算法sort支持对结构体排序,我们只需要构造出一个满足上述优先级的布尔函数cmp().
#include <bits/stdc++.h>
using namespace std;
struct node//传建一个吉利的结构体数组用来储存学生成绩
{
int id;
int x1, x2, x3;
int tot;
}p[666];
bool cmp(const node& A, const node& B) //满足题目要求的优先级cmp函数
{
if(A.tot > B.tot) return true;
else if(A.tot < B.tot) return false;
else if(A.x1 > B.x1) return true;
else if(A.x1 < B.x1) return false;
else if(A.id < B.id) return true;
else return false;
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) //input
{
p[i].id = i + 1;
scanf("%d %d %d", &p[i].x1, &p[i].x2, &p[i].x3);
p[i].tot = p[i].x1 + p[i].x2 + p[i].x3;
}
sort(p, p+n, cmp); //排序先!
for(int i = 0; i < 5; i++) //output
{
printf("%d %d\n", p[i].id, p[i].tot);
}
return 0;
}