题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7

双循环吧。。

#include <stdio.h>
#include <stdlib.h>
struct exam_info {
    int num;
    int score;
};

void ascend_method(struct exam_info* temp, int start, int end, int method) {
    if (method) {
        for (int i = start; i <= end; i++) {
            for (int j = start; j <= end - 1; j++) {
                if (temp[j].score > temp[j + 1].score) {
                    struct exam_info t = temp[j + 1];
                    temp[j + 1] = temp[j];
                    temp[j] = t;
                }
            }
        }
    } else {
        for (int i = start; i <= end; i++) {
            for (int j = start; j <= end - 1; j++) {
                if (temp[j].num > temp[j + 1].num) {
                    struct exam_info t = temp[j + 1];
                    temp[j + 1] = temp[j];
                    temp[j] = t;
                }
            }
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    struct exam_info stu_info[n];
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &stu_info[i].num, &stu_info[i].score);
    }
    ascend_method(stu_info, 0, n - 1, 1);
    int j = 0, k = 0;
    while (j != n) {
        if (stu_info[j].score == stu_info[k].score) {
            while (stu_info[j].score == stu_info[k].score && k != n) k++;
            ascend_method(stu_info, j, k - 1, 0);
            j = k;
            k++;
        } else {
            j++;
            k++;
        }
    }
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", stu_info[i].num, stu_info[i].score);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务