题解 | 小白鼠排队

小白鼠排队

https://www.nowcoder.com/practice/27fbaa6c7b2e419bbf4de8ba60cf372b

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
    int weight;
    char color[11]; 
} Rat;
int compare(const void* a, const void* b) {
    Rat* rat1 = (Rat*)a;
    Rat* rat2 = (Rat*)b;
    return rat2->weight - rat1->weight; 
}
int main() {
    int N;
    while (scanf("%d", &N) != EOF) { 
        Rat rats[100]; 
        for (int i = 0; i < N; i++) {
            scanf("%d %s", &rats[i].weight, rats[i].color); 
        }
        qsort(rats, N, sizeof(Rat), compare);
        for (int i = 0; i < N; i++) {
            printf("%s\n", rats[i].color);
        }
    }
    return 0;
}

qsort+数据结构 easy

全部评论

相关推荐

仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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