题解 | #成绩排序#

成绩排序

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

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务