用C语言编写程序,找出总分最高的学生,建立一个有n个学生成绩的结构记录,包括学号姓名和三科成绩,输出总分最高学生的姓名和总分
时间: 2023-09-10 17:15:28 浏览: 153
以下是用C语言编写的程序,可以找出总分最高的学生:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最大学生数
// 学生结构体
typedef struct Student {
char id[10]; // 学号
char name[20]; // 姓名
int score1; // 第一科成绩
int score2; // 第二科成绩
int score3; // 第三科成绩
int totalScore; // 总分
} Student;
int main() {
int n; // 学生数
Student students[MAX_STUDENTS]; // 学生结构体数组
int maxScore = 0; // 最高分
int maxIndex = 0; // 最高分学生的下标
// 输入学生数和各学生信息
printf("请输入学生数:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的学号、姓名和三科成绩:", i + 1);
scanf("%s%s%d%d%d", students[i].id, students[i].name, &students[i].score1, &students[i].score2, &students[i].score3);
students[i].totalScore = students[i].score1 + students[i].score2 + students[i].score3; // 计算总分
}
// 查找总分最高的学生
for (int i = 0; i < n; i++) {
if (students[i].totalScore > maxScore) {
maxScore = students[i].totalScore;
maxIndex = i;
}
}
// 输出总分最高学生的姓名和总分
printf("总分最高的学生是%s,总分为%d分。\n", students[maxIndex].name, maxScore);
return 0;
}
```
该程序先输入学生数和各学生信息,然后计算出每个学生的总分,并查找总分最高的学生,最后输出总分最高学生的姓名和总分。