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;
}
相关推荐
努力学算法的蒟蒻23 分钟前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_8414956427 分钟前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦29 分钟前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
CodeSheep程序羊1 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
2401_841495641 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
budingxiaomoli1 小时前
优选算法-字符串
算法
I'mChloe1 小时前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言
qq7422349841 小时前
APS系统与OR-Tools完全指南:智能排产与优化算法实战解析
人工智能·算法·工业·aps·排程
2的n次方_1 小时前
Runtime 内存管理深化:推理批处理下的内存复用与生命周期精细控制
c语言·网络·架构
嵌入小生0072 小时前
标准IO---核心函数接口延续(嵌入式Linux)
c语言·vscode·vim·嵌入式·小白·标准io·函数接口