#include <stdio.h> #define NUM_STUDENTS 5 #define NUM_COURSES 3

void findHighestTotalScore(int scores[][NUM_COURSES], int* highestIndex, int* highestTotal) { *highestTotal = 0;

for (int i = 0; i < NUM_STUDENTS; i++) 
{
    int total = 0;
    for (int j = 0; j < NUM_COURSES; j++) 
	{
        total += scores[i][j];
    }

    if (total > *highestTotal) 
	{
        *highestTotal = total;
        *highestIndex = i;
    }
}

}

int main() { int scores[NUM_STUDENTS][NUM_COURSES];

printf('请输入每个学生的学号和三门课程的分数:\n');
for (int i = 0; i < NUM_STUDENTS; i++) {
    printf('学生 %d:\n', i + 1);
    printf('学号:');
    scanf('%d', &scores[i][0]);
    printf('分数:');
    for (int j = 1; j <= NUM_COURSES; j++) {
        scanf('%d', &scores[i][j]);
    }
}

int highestIndex, highestTotal;
findHighestTotalScore(scores, &highestIndex, &highestTotal);
printf('三门课程总分最高的学生学号:%d\n', scores[highestIndex][0]);
printf('每门课的成绩:');
for (int j = 1; j <= NUM_COURSES; j++) {
    printf('%d ', scores[highestIndex][j]);
}
printf('\n');

return 0;

}

这段代码是一个简单的学生成绩管理程序,通过输入每个学生的学号和三门课程的分数,找出三门课程总分最高的学生,并输出其学号和每门课的成绩。

程序首先定义了两个宏常量,NUM_STUDENTS表示学生的数量,NUM_COURSES表示课程的数量。

函数findHighestTotalScore用于找出三门课程总分最高的学生。它首先将最高总分初始化为0,然后遍历每个学生,计算其三门课程的总分。如果当前学生的总分大于最高总分,则更新最高总分和最高总分学生的索引。

主函数main负责输入学生的学号和分数,并调用findHighestTotalScore函数找出最高总分学生。然后,它输出最高总分学生的学号和每门课的成绩。

这段代码比较简单,主要涉及到二维数组的使用和函数的调用。


原文地址: https://www.cveoy.top/t/topic/pBWI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录