题解 | #成绩排序#

成绩排序

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

冒泡排序

#include <stdio.h>
#include <stdlib.h>
struct exam_info {
    char name[10];
    int score;
};

void ascend(struct exam_info* temp, int num) {
    for (int i = 0; i < num-1; i++) {
        for (int j = 0; j < num-i-1; j++) {
            if (temp[j].score > temp[j+1].score) {
                struct exam_info t = temp[j];
                temp[j] = temp[j+1];
                temp[j+1] = t;
            }
        }
    }
}
void descend(struct exam_info* temp, int num) {
    for (int i = 0; i < num-1; i++) {
        for (int j = 0; j < num-i-1; j++) {
            if (temp[j].score < temp[j+1].score) {
                struct exam_info t = temp[j];
                temp[j] = temp[j+1];
                temp[j+1] = t;
            }
        }
    }
}

void input_score(struct exam_info* temp, int num){
    for(int i = 0;i<num;i++){
        printf("%s %d\n",temp[i].name,temp[i].score);
    }
}

int main() {
    int num;
    int method;
    while(scanf("%d",&num)!=EOF){
        scanf("%d", &method);
        struct exam_info stu_info[num];
        for (int i = 0; i < num; i++) {
            scanf("%s %d", stu_info[i].name, &stu_info[i].score);
        }
        if (method) {
            ascend(stu_info, num);
            input_score(stu_info,num);
        } else {
            descend(stu_info, num);
            input_score(stu_info,num);
        }
    }
    return 0;
}

全部评论

相关推荐

码农索隆:传音老登来也。 但是这个我不知道怎么回答,不仅仅传音吧,很多公司在候选人不第一时间接受offer或主动将报道时间延期时,都会再从池子里面捞人,直到l捞到满足公司所有要求的人。
秋招的第一个offer,...
点赞 评论 收藏
分享
用微笑面对困难:这里面最强的是驾驶证了,可以入职美团大厂,然后直接开启黄马褂人生
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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