C语言练习(28)

有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课程的成绩、平均分数)。

要求用一个input函数输入10个学生数据,用一个average函数求总平均分,用max函数找出最高分学生数据,总平均分和最高分的学生的数据都在主函数中输出。

复制代码
#include <stdio.h>
#include <string.h>

// 定义结构体
struct Student {
    int id;
    char name[20];
    int scores[3];
    double average_score;
};

// 输入函数
void input(struct Student students[], int n) {
    int i, j;
    for (i = 0; i < n; i++) {
        printf("请输入第 %d 个学生的学号: ", i + 1);
        scanf_s("%d", &students[i].id);
        printf("请输入第 %d 个学生的姓名: ", i + 1);
        scanf_s("%s", students[i].name);
        for (j = 0; j < 3; j++) {
            printf("请输入第 %d 个学生的第 %d 门课程成绩: ", i + 1, j + 1);
            scanf_s("%d", &students[i].scores[j]);
        }
    }
}

// 求平均成绩函数
double average(struct Student students[], int n) {
    int i, j;
    double sum = 0;
    for (i = 0; i < n; i++) {
        for (j = 0; j < 3; j++) {
            sum += students[i].scores[j];
        }
    }
    return sum / (n * 3);
}

// 找最高分学生函数
struct Student max(struct Student students[], int n) {
    int i, j;
    struct Student max_student = students[0];
    for (i = 0; i < n; i++) {
        int total_score = 0;
        for (j = 0; j < 3; j++) {
            total_score += students[i].scores[j];
        }
        if (total_score > max_student.average_score * 3) {
            max_student = students[i];
        }
    }
    // 计算平均分
    max_student.average_score = (double)(max_student.scores[0] + max_student.scores[1] + max_student.scores[2]) / 3;
    return max_student;
}

int main() {
    struct Student students[10];
    // 输入学生数据
    input(students, 10);
    // 求总平均成绩
    double avg = average(students, 10);
    // 找最高分学生
    struct Student max_student = max(students, 10);
    // 输出总平均成绩
    printf("3门课程总平均成绩为: %.2f\n", avg);
    // 输出最高分学生数据
    printf("最高分学生数据:\n");
    printf("学号: %d\n", max_student.id);
    printf("姓名: %s\n", max_student.name);
    printf("3门课程成绩: %d, %d, %d\n", max_student.scores[0], max_student.scores[1], max_student.scores[2]);
    printf("平均分数: %.2f\n", max_student.average_score);
    return 0;
}
相关推荐
码完就睡13 分钟前
数据结构——树、二叉树基础概念
数据结构·算法
VL——MOESR1 小时前
【LuoguP1967】货车运输【生成树】【倍增】
c++·算法·题解·倍增·生成树
手写码匠1 小时前
华为云Flexus+DeepSeek征文|Agent 记忆系统实战:用 DeepSeek-R1/V3 + Dify 会话变量打造跨会话长期记忆
人工智能·深度学习·算法·aigc
Olafur_zbj1 小时前
【AI】CUDA编程中的维度
人工智能·算法
十月的皮皮2 小时前
STM32从零到量产开发:四路继电器工业控制模块开发 -上位机 Program.cs 应用程序入口设计说明
c语言·stm32·单片机·stm32cubemx·hal库
坚持编程的菜鸟2 小时前
模拟实现memcpy
c语言·算法·模拟实现my_memcpy
wabs6662 小时前
关于图论【最短路径之Bellman_ford 算法|卡码网94.城市间货物运输的思考】
数据结构·算法·图论·卡码网·bellman_ford·求最短路径
MrZhao4002 小时前
On-Policy Distillation(OPD):为什么大模型后训练要在学生自己的轨迹上蒸馏?
算法
小小龙学IT2 小时前
Day 28 项目调试与优化 —— 给聊天室做一次“全面体检“
c语言·开发语言
朱峥嵘(朱髯)2 小时前
数据库如何根据全表 NDV 估算子集的 NDV
数据库·算法