分步实践任务说明
共 3 个核验步骤01
步骤 1:动态分配结构体数组并按综合成绩降序输出
读入学生人数 `n` 及随后 `n` 行 `id code regular final_exam`,使用 `malloc` 动态分配 `Student` 数组并判空,计算 `total = regular * 0.4 + final_exam * 0.6`,按 `total` 降序输出每行 `%d %s %.2f\n`,末行输出 `AVG=%.2f\n` 并 `free`。
02
步骤 2:完善同分按学号升序的次关键字排序规则
当两位学生的综合成绩之差满足 `fabs(a->total - b->total) <= 1e-9` 时,按学号 `id` 升序排列,避免直接用 `==` 比较浮点数。
03
步骤 3:通过空规模与单记录边界检查
当输入人数 `n <= 0` 时不调用 `malloc`,直接输出 `EMPTY\n`;确保 `n == 1` 与多条混合数据也能零泄漏通过。
起步代码框架预览
C 语言#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
int id;
char code[16];
double regular;
double final_exam;
double total;
} Student;
int main(void) {
int n = 0;
if (scanf("%d", &n) != 1) {
return 0;
}
/* TODO: 1) 若 n <= 0 输出 EMPTY\n 并结束 */
/* TODO: 2) 用 malloc 动态分配 n 个 Student 并判空,读入并计算 total = regular*0.4 + final_exam*0.6 */
/* TODO: 3) 按 total 降序、差值 <= 1e-9 时按 id 升序排序并输出,最后打印 AVG=%.2f\n 并 free */
return 0;
}