华为机试C语言-按身高和体重排队

题目描述:https://blog.csdn.net/qq_23934063/article/details/124722680

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct Node {
    int high;
    int weight;
    int sort;
};

int myCmp(const void* a, const void* b) {
    struct Node* aa = (struct Node*)a;
    struct Node* bb = (struct Node*)b;

    if (aa->high != bb->high) {
        return aa->high - bb->high;
    } else {
        return aa->weight - bb->weight;
    }
}

void str_handler(char* str_H, char* str_W, struct Node* node) {
    char* tmp;
    int index = 0;
    tmp = strtok(str_H, " ");
    node[index].sort = index + 1;
    node[index++].high = atoi(tmp);

    while (1) {
        tmp = strtok(NULL, " ");
        if (tmp == NULL) {
            break;
        }

        node[index].sort = index + 1;
        node[index++].high = atoi(tmp);
    }

    index = 0;
    tmp = strtok(str_W, " ");
    node[index++].weight = atoi(tmp);

    while (1) {
        tmp = strtok(NULL, " ");
        if (tmp == NULL) {
            break;
        }

        node[index++].weight = atoi(tmp);
    }
}

int main() {
    struct Node* node = NULL;
    char str1[1000] = {0};
    char str2[1000] = {0};
    int num;

    scanf("%d", &num);
    getchar();
    node = (struct Node*)malloc(sizeof(struct Node) * num);

    gets(str1);
    gets(str2);

    str_handler(str1, str2, node);

    qsort(node, num, sizeof(node[0]), myCmp);

    for (int i = 0; i < num - 1; i++) {
        printf("%d ", node[i].sort);
    }
    printf("%d\n", node[num - 1].sort);


    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值