题目描述: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;
}