解题思路:
结构体+快速排序
参考代码:#include
#include
struct stu
{
int num;//学号
int a, b, c;//三科成绩
int sum;//总分
};
int cmp(const void *a, const void *b)//自定义的比较方法
{
int t1 = (*(struct stu *)b).sum - (*(struct stu *)a).sum;
if(t1 == 0)
{
int t2 = (*(struct stu *)b).a - (*(struct stu *)a).a;
if(t2 == 0)
{
return (*(struct stu *)a).num - (*(struct stu *)a).num;
}
else
return t2;
}
else
return t1;
}
int main()
{
int n;
scanf("%d", &n);
struct stu st[n];
for(int i=0; i
{
scanf("%d %d %d", &st[i].a, &st[i].b, &st[i].c);
st[i].num = i+1;
st[i].sum = st[i].a + st[i].b + st[i].c;
}
qsort(st,n,sizeof(st[0]),cmp);//快速排序
for(int i=0; i<5; i++)
{
printf("%d %d\n",st[i].num,st[i].sum);
}
return 0;
}