有10个学生的信息已被放入结构体数组a中,编程输出成绩最高学生的学号和姓名

img

img


有10个学生的信息已被放入结构体数组a中,编程输出成绩最高学生的学号和姓名

供参考:

#include <stdio.h>
struct student {
    int    num;
    char   name[20];
    double score;
}a[10] = { {101,"张飞",83},  {102,"李逵",29},  
           {103,"阿斗",35},  {104,"孙权",77}, 
           {105,"曹操",65},  {106,"诸葛亮",90},
           {107,"袁绍",49},  {108,"赵子龙",80},
           {109,"赵本山",89},{110,"潘长江",68}
};
int main()
{
    int  i;
    double max;
    for (i = 0, max = a[i].score; i < 10; i++)
        if (a[i].score > max)  max = a[i].score;
    for (i = 0; i < 10; i++)
        if (a[i].score == max)
            printf("%d %s\n", a[i].num, a[i].name);
    return 0;
}

可采纳

#include <stdio.h>
typedef struct {
    int id;
    char name[20];
    int score;
} Student;

int main() {
    Student a[10] = {{1, "张三", 90}, {2, "李四", 85}, {3, "王五", 92}, {4, "赵六", 88}, {5, "陈七", 95}, {6, "孙八", 82}, {7, "周九", 91}, {8, "吴十", 89}, {9, "郑十一", 93}, {10, "黄十二", 86}};
    int max_score = a[0].score;
    int max_id = a[0].id;
    for (int i = 1; i < 10; i++) {
        if (a[i].score > max_score) {
            max_score = a[i].score;
            max_id = a[i].id;
        }
    }
    printf("成绩最高学生的学号:%d,姓名:%s", max_id, a[max_id].name);
    return 0;
}