题解 | 小白鼠排队

小白鼠排队

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

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务