题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

#include <stdio.h>
#include <string.h>
typedef struct studentStruct{
    int value;
    int number;
    char name[50];
}student;
int main() {
    student tableStudent[200];

    int N, arr;
    scanf("%d %d",&N,&arr);
    for (int i=0; i<N; i++) {
        scanf("%s %d",tableStudent[i].name,&tableStudent[i].value);
        tableStudent[i].number = i;
    }

    for (int i=0; i<N; i++) {
        for (int j=i+1; j<N; j++) {
            student tempStudent = tableStudent[i];
            if(!arr){
                if(tempStudent.value<tableStudent[j].value) {
                    tableStudent[i] = tableStudent[j];
                    tableStudent[j] = tempStudent;
                }
            }else{
                if(tempStudent.value>tableStudent[j].value) {
                    tableStudent[i] = tableStudent[j];
                    tableStudent[j] = tempStudent;
                }
            }
            
        }
    }

    for (int i=0; i<N; i++) {
        for (int j=i+1; j<N; j++) {
            student tempStudent = tableStudent[i];
            if(tempStudent.value != tableStudent[j].value) continue;
            if(tempStudent.number > tableStudent[j].number) {
                    tableStudent[i] = tableStudent[j];
                    tableStudent[j] = tempStudent;
            }
        }
    }

    for (int i=0; i<N; i++) {
        printf("%s %d\n",tableStudent[i].name,tableStudent[i].value);
    }
    return 0;
}

全部评论

相关推荐

09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
09-17 19:25
已编辑
太原理工大学 游戏测试
叁六玖:公司名发我,我要这个HR带我打瓦
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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