C语言学生成绩管理:查找最高分学生信息
#include <stdio.h> #define M 20
typedef struct student { char id[6]; char name[10]; int a, b, c; long int sum; } STU;
int main() { STU std[M]; int i, N, max, max_j; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%s %s %d %d %d", std[i].id, std[i].name, &std[i].a, &std[i].b, &std[i].c); std[i].sum = std[i].a + std[i].b + std[i].c; // 计算总分 } max = std[0].sum; max_j = 0; for (i = 1; i < N; i++) { if (max < std[i].sum) { max = std[i].sum; max_j = i; } } printf("%s %s %ld\n", std[max_j].id, std[max_j].name, std[max_j].sum); return 0; }
主要修改如下:
- 在输入学生信息时,将计算总分的代码放到循环中。
- 在计算最高分时,将比较条件修改为“max < std[i].sum”。
- 将输出学生信息的代码中的“std[1]”修改为“std[max_j]”。
原文地址: https://www.cveoy.top/t/topic/ofJu 著作权归作者所有。请勿转载和采集!