关于c语言数组和循环

请分别找出数学,语文成绩的最高分及对应的学号(注意:最高分可能不止一名同学)。
学号 数学 语文
1 76 89
2 100 91
3 56 79
4 76 65
5 98 64
6 100 62
7 51 87
8 77 83
9 72 82
10 61 71
11 62 78
12 79 77
13 65 73
14 64 91
15 83 65
16 82 60
17 71 66
18 89 78
19 91 72
20 79 71

运行结果如下:

img

代码:

#include <stdio.h>
int main()
{
    int score[][3] = { 
        {1,76,89}, //分别代表学号、数学成绩、语文成绩
        {2,100,91},
        {3,56,79},
        {4,76,65 },
        {5, 98, 64},
        {6,100,62},
        {7,51,87},
        {8,77,83},
        {9,72,82},
        {10,61,71},
        {11,62,78},
        {12,79,77},
        {13,65,73},
        {14,64,91},
        {15,83,65},
        {16,82,60},
        {17,71,66},
        {18,89,78},
        {19,91,72},
        {20,79,71}
    };
    int maxChinese = 0, maxMath = 0;
    int i = 0;
    for (; i < 20; i++)
    {
        if (score[i][1] > score[maxMath][1]) //找出数学成绩最高的学号
            maxMath = i;
        if (score[i][2] > score[maxChinese][2])//找出数学成绩最高的学号
            maxChinese = i;
    }
    printf("数学成绩最高分:%d\n",score[maxMath][1]);
    printf("学号分别为:");
    for (i = maxMath; i < 20; i++)
    {
        if (score[i][1] == score[maxMath][1])
            printf("%d ", score[i][0]);
    }
    printf("\n");

    printf("语文成绩最高分:%d\n", score[maxChinese][2]);
    printf("学号分别为:");
    for (i = maxChinese; i < 20; i++)
    {
        if (score[i][2] == score[maxChinese][2])
            printf("%d ", score[i][0]);
    }
    printf("\n");
    return 0;
}

到底要啥语言啊?
这些成绩是固定的,还是要输入呢?

该回答引用GPTᴼᴾᴱᴺᴬᴵ

scores = [
    [1, 76, 89],
    [2, 100, 91],
    [3, 56, 79],
    [4, 76, 65],
    [5, 98, 64],
    [6, 100, 62],
    [7, 51, 87],
    [8, 77, 83],
    [9, 72, 82],
    [10, 61, 71],
    [11, 62, 78],
    [12, 79, 77],
    [13, 65, 73],
    [14, 64, 91],
    [15, 83, 65],
    [16, 82, 60],
    [17, 71, 66],
    [18, 89, 78],
    [19, 91, 72],
    [20, 79, 71]
]

math_scores = []
chinese_scores = []

for score in scores:
    math_scores.append(score[1])
    chinese_scores.append(score[2])

max_math_score = max(math_scores)
max_chinese_score = max(chinese_scores)

students_with_max_math_score = [score[0] for score in scores if score[1] == max_math_score]
students_with_max_chinese_score = [score[0] for score in scores if score[2] == max_chinese_score]

print("数学最高分为{},对应的学号是{}".format(max_math_score, students_with_max_math_score))
print("语文最高分为{},对应的学号是{}".format(max_chinese_score, students_with_max_chinese_score))


输出:

数学最高分为100,对应的学号是[2, 6]
语文最高分为91,对应的学号是[2, 14]


如果有帮助到你,请采纳~

#include <stdio.h>

int main() {
    int math_score[] = {76, 100, 56, 76, 98, 100, 51, 77, 72, 61, 62, 79, 65, 64, 83, 82, 71, 89, 91, 79};
    int chinese_score[] = {89, 91, 79, 65, 64, 62, 87, 83, 82, 71, 78, 77, 73, 91, 65, 60, 66, 78, 72, 71};
    int max_math_score = 0;
    int max_chinese_score = 0;
    int max_math_score_id[20] = {0};  // 假设最高分的学号不超过20个
    int max_chinese_score_id[20] = {0};  // 假设最高分的学号不超过20个
    int i;

    for (i = 0; i < 20; i++) {
        // 检查数学成绩
        if (math_score[i] > max_math_score) {
            max_math_score = math_score[i];
            max_math_score_id[0] = i + 1;
            // 将最高分学生的学号清空,因为可能不止一个人成绩最高
            int j;
            for (j = 1; j < 20; j++) {
                max_math_score_id[j] = 0;
            }
        } else if (math_score[i] == max_math_score) {
            // 如果有多个学生数学成绩最高,则将他们的学号都记录下来
            max_math_score_id[i] = i + 1;
        }

        // 检查语文成绩
        if (chinese_score[i] > max_chinese_score) {
            max_chinese_score = chinese_score[i];
            max_chinese_score_id[0] = i + 1;
            // 将最高分学生的学号清空,因为可能不止一个人成绩最高
            int j;
            for (j = 1; j < 20; j++) {
                max_chinese_score_id[j] = 0;
            }
        } else if (chinese_score[i] == max_chinese_score) {
            // 如果有多个学生语文成绩最高,则将他们的学号都记录下来
            max_chinese_score_id[i] = i + 1;
        }
    }

    // 输出数学成绩最高的学生的学号
    printf("Math Score:\n");
    printf("The highest score is %d, and the students with the highest score are: ", max_math_score);
    for (i = 0; i < 20; i++) {
        if (max_math_score_id[i] != 0) {
            printf("%d ", max_math_score_id[i]);
        }
    }
    printf("\n");

    // 输出语文成绩最高的学生的学号
    printf("Chinese Score:\n");
    printf("The highest score is %d, and the students with the highest score are: ", max_chinese_score);
    for (


您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632