题解 | #成绩排序#

成绩排序

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

全部评论

相关推荐

讲原则的小黄鸭不愿吃...:有时候面试眼缘确实很重要,当然,飞驰人生2中张弛说的很对:我努力了无数次,但是我知道机会只会出现在其中一两次。你把每一次笔试面试都全力以赴,总有你运气发挥到位的时候
点赞 评论 收藏
分享
牛客41077653...:想问一下华为池子是不是很大呀
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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