题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include <stdio.h>
#include <string.h>

typedef struct student {
    int score;
    char name[200];
} student;

void bubbleSort(int *a, int n, int kind) {
    int temp;
    if (kind == 0) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] < a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
    if (kind == 1) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
}

int main() {
    int n, kind;
    while (scanf("%d %d", &n, &kind) != EOF) {
        int a[200];
        student stu[200];
        for (int i = 0; i < n; i++) {
            scanf("%s %d", stu[i].name, &stu[i].score);
            a[i] = stu[i].score;
        }

        bubbleSort(a, n, kind);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (a[i] == stu[j].score) {
                    printf("%s %d\n", stu[j].name, stu[j].score);
                    stu[j].score = -1; // 标记该学生已经输出过
                    break;
                }
            }
        }
    }

    return 0;
}

全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务