R7-1 学生平均成绩排序

简介: R7-1 学生平均成绩排序

假设学生的基本信息包括学号、姓名、三门课程成绩以及个人平均成绩,定义一个能够表示学生信息的结构类型。输入n(n<50)个学生的成绩信息,按照学生的个人平均分从高到低输出他们的信息。


注意:

1)平均分出现相同的分数时按学号从小到大进行排序输出。

2)平均分以四舍五入取整数保存。


输入格式:

输入一个正整数n(n<50),下面n行输入n个学生的信息,包括:学号、姓名、三门课程成绩(整数)。


输出格式:

输出从高到低排序后的学生信息,包括:学号、姓名、三门课程成绩、平均分(整数)


输入样例:

4
101 Zhang 78 87 85
102 Wang 91 88 90
104 chen 86 90 75
103 Li 75 90 86


输出样例:

102 Wang 91 88 90 90
103 Li 75 90 86 84
104 chen 86 90 75 84
101 Zhang 78 87 85 83


我的做法:


结构体版:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
 
typedef struct student {
    int id;
    char name[20];
    int score1;
    int score2;
    int score3;
    int average;
} Student;
 
// 以平均分和学号为关键字进行比较,返回值为比较结果
int cmp(const void* a, const void* b) {
    const Student* s1 = (Student*)a;
    const Student* s2 = (Student*)b;
    if (s1->average != s2->average) {
        return s2->average - s1->average;
    }
    else {
        return s1->id - s2->id;
    }
}
 
int main() {
    int n;
    scanf("%d", &n);
    Student stu[n]; // 定义一个结构体数组
    for (int i = 0; i < n; i++) {
        // 输入学生信息并计算平均分
        scanf("%d %s %d %d %d", &stu[i].id, stu[i].name, &stu[i].score1, &stu[i].score2, &stu[i].score3);
        int sum = stu[i].score1 + stu[i].score2 + stu[i].score3;
        stu[i].average = round(1.0 * sum / 3); // 平均分四舍五入取整数
    }
    qsort(stu, n, sizeof(Student), cmp); // 调用库函数快速排序
 
    // 输出排过序的学生信息
    for (int i = 0; i < n; i++) {
        printf("%d %s %d %d %d %d\n", stu[i].id, stu[i].name, stu[i].score1, stu[i].score2, stu[i].score3, stu[i].average);
    }
    return 0;
}


不用结构体的版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
 
int id[50];
char name[50][20];
int score1[50], score2[50], score3[50];
int average[50];
 
// 以平均分和学号为关键字进行比较,返回值为比较结果
int cmp(const void* a, const void* b) {
    const int i = *(int*)a;
    const int j = *(int*)b;
    if (average[i] != average[j]) {
        return average[j] - average[i];
    }
    else {
        return id[i] - id[j];
    }
}
 
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        // 输入学生信息并计算平均分
        scanf("%d %s %d %d %d", &id[i], name[i], &score1[i], &score2[i], &score3[i]);
        int sum = score1[i] + score2[i] + score3[i];
        average[i] = round(1.0 * sum / 3); // 平均分四舍五入取整数
    }
    int index[n]; // 定义一个索引数组
    for (int i = 0; i < n; i++) {
        index[i] = i; // 初始化索引数组
    }
    qsort(index, n, sizeof(int), cmp); // 调用库函数快速排序
 
    // 输出排过序的学生信息
    for (int i = 0; i < n; i++) {
        printf("%d %s %d %d %d %d\n", id[index[i]], name[index[i]], score1[index[i]], score2[index[i]], score3[index[i]], average[index[i]]);
    }
    return 0;
}
相关文章
|
17天前
|
人工智能
第4章-7 统计学生平均成绩与及格人数
该程序计算一组学生的平均成绩和及格人数。输入包含学生人数N和N个成绩,输出格式为&quot;average = 平均成绩&quot;和&quot;count = 及格人数&quot;。提供的代码首先读取学生数量,然后累加成绩并计数及格者。平均成绩保留一位小数。当学生人数为0时,平均成绩和及格人数分别输出0.0和0。
24 1
|
4月前
计算三个同学的总成绩、平均成绩。
计算三个同学的总成绩、平均成绩。
49 0
|
3月前
|
存储 算法 搜索推荐
百万考生分数如何排序 - 计数排序
百万考生分数如何排序 - 计数排序
24 0
|
3月前
计蒜客_成绩排序
计蒜客_成绩排序
|
9月前
1178:成绩排序
1178:成绩排序
|
8月前
|
C++
成绩统计
小蓝给学生们组织了一场考试,卷面总分为100分,每个学生的得分都是一个0到100的整数。如果得分至少是60分,则称为及格。
62 0
|
5月前
某学科成绩的录入并显示出最高分,最低分,平均值。
某学科成绩的录入并显示出最高分,最低分,平均值。
|
9月前
定义数组,存放5个学生的成绩(成绩值自己设定),将成绩从大到小排序,计算并显示输出成绩总和,平均成绩,最小成绩,最大成绩。
定义数组,存放5个学生的成绩(成绩值自己设定),将成绩从大到小排序,计算并显示输出成绩总和,平均成绩,最小成绩,最大成绩。
331 0
成绩排序 给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
成绩排序 给出班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
399 0