题解 | #重理轻文#
重理轻文
https://ac.nowcoder.com/acm/contest/70264/C
//编程新手,如有错误,恳请指正
#include<stdio.h>
#include<stdlib.h>
struct stu//结构体创建
{
char name[10];
int math;
int chi;
int eng;
}stu;
int compare1(const void* a, const void* b)//总分排序
{
struct stu* pa = (struct stu*)a;
struct stu* pb = (struct stu*)b;
int num1 = pa->chi + pa->eng + pa->math;
int num2 = pb->chi + pb->eng + pb->math;
return num2 - num1;
}
int compare2(const void* a, const void* b)//****数学优先排序****
{
struct stu* pa = (struct stu*)a;
struct stu* pb = (struct stu*)b;
int num1 = pa->math;//数学
int num2 = pb->math;
int num3 = pa->chi + pa->eng + pa->math;//总分
int num4 = pb->chi + pb->eng + pb->math;
if(num3==num4)//总分相同排数学
{
return num2 - num1;
}
else//总分不同排序不变,依compare1排序
{
return 0;
}
}
int main()
{
int n = 0;
struct stu arr[100] = {0};//结构体数组创建
scanf("%d", &n);//录入个数
for (int i = 0; i < n; i++)//输入
{
scanf("%s", &arr[i].name);
scanf("%d", &arr[i].math);
scanf("%d", &arr[i].chi);
scanf("%d", &arr[i].eng);
}
qsort(arr, n, sizeof(struct stu), compare1);//总分排序
qsort(arr, n, sizeof(struct stu), compare2);//数学优先排序
for (int i = 0; i < n; i++)//输出
{
printf("%s ", arr[i].name);
}
return 0;
}
查看12道真题和解析
