打印任务排序

题目描述:https://blog.csdn.net/weixin_44052055/article/details/125812220?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2aggregatepagefirst_rank_ecpm_v1~rank_v31_ecpm-3-125812220-null-null.pc_agg_new_rank&utm_term=Java%E6%89%93%E5%8D%B0%E4%BB%BB%E5%8A%A1%E6%8E%92%E5%BA%8F&spm=1000.2123.3001.4430

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

#define QUEUE_MAX  1000

struct node {
    int val;
    int order;
};

int hash_find_max(int *hash, int len)
{
    for (int i = len - 1; i > 0; i--) {
        if (hash[i] != 0) {
            return i;
        }
    }
    
    return 0;
}

int main(void)
{
    struct node queue[QUEUE_MAX] = {0};
    int hash[10] = {0};
    char str[1000] = {0};
    char *tmp;
    int index = 0;
    int i = 0;
    int max_in_queue;
    int res[1000] = {0};
    int res_index = 0;
    int res_size;
    
    gets(str);
    printf("%s\n", str);
    
    tmp = strtok(str, ",");
    queue[index].val = atoi(tmp);
    hash[queue[index].val]++;
    printf("index=%d, %s, %d\n", index, tmp, queue[index].val);
    queue[index++].order = index;
    
    while (1) {
        tmp = strtok(NULL, ",");
        if (tmp == NULL) break;
        queue[index].val = atoi(tmp);
        hash[queue[index].val]++;
        printf("index=%d, %s, %d\n", index, tmp, queue[index].val);
        queue[index++].order = index;
    }
    
    res_size = index;
    
    while (i < index) {
        max_in_queue = hash_find_max(hash, 10);
        if (queue[i].val == max_in_queue) {
            res[res_index++] = queue[i].order;
            hash[queue[i].val]--;
        } else {
            queue[index].val = queue[i].val;
            queue[index++].order = queue[i].order;
        }
        i++;
    }
    
    printf("%d", res[0]);
    for (int j = 1; j < res_size; j++) {
        printf(",%d", res[j]);
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值