题解 | #成绩排序#

成绩排序

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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
01-10 16:40
点赞 评论 收藏
分享
2025-12-29 20:37
已编辑
清华大学附属小学 Java
开始打牌offer啦:1.为什么要写这么多内容呀 2.什么叫做简历 3.什么样的内容可以写到简历上 4.项目可以包装,但是要有理有据呀,不能乱包装呀,比如 跨境能达到日均120万订单的在国内都是能叫的上名字的,而且这些工作也基本上不太会交给一个实习生去做 建议友友可以去网上或者找同学的简历看看,他们的简历是怎么写的,去找找上面的那四个问题的答案吧,然后要记住的是Java是服务于业务的,而不是服务于微服务或者技术的
最后再改一次简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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