题解 | #成绩排序#

成绩排序

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

全部评论

相关推荐

牛客100866号技...:把电科加粗,把电科加粗,把电科加粗,两个吊车尾的项目合并成一个,再加一个管理系统。电科✌🏻在成都面中厂手拿把掐
点赞 评论 收藏
分享
05-29 19:16
已编辑
福建农林大学 测试开发
努力勤奋的马洛格已躺...:翻译:面试前没盘点好hc一下面太多了,现在在排序回去等通知
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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