蚂蚁爬树问题希望能解决

有k个蜗牛,各有它们不同的爬行速度,通常都是从树根向上爬,若树高为h米,如第i只蜗牛按它的速度每次向上爬ni米,向下滑mi米.试输出每只蜗牛直到爬到树顶的过程中爬过每一米线经过的次数 。统计树的每一米线都有多少次蜗牛爬过。要求:采用链表实现.采用顺序栈实现哪只蜗牛爬得最快,请输出它的爬行速度规律。

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

#define MAX_SIZE 100

// 定义蜗牛信息的结构体
struct Snail
{
    int ni;     // 爬行速度 ni
    int mi;     // 滑行速度 mi
    int height; // 当前高度
    int time;   // 爬完整棵树所需的时间
};

// 定义链表结点
struct Node
{
    struct Snail snail;
    struct Node *next;
};

// 定义顺序栈
struct Stack
{
    int top;
    int data[MAX_SIZE];
};

// 创建链表
struct Node *createList(int n)
{
    struct Node *head = NULL;
    struct Node *p = NULL;

    for (int i = 0; i < n; i++)
    {
        struct Snail snail;
        printf("输入第 %d 只蜗牛的爬行速度 ni 和滑行速度 mi: ", i + 1);
        scanf("%d%d", &snail.ni, &snail.mi);
        snail.height = 0;
        snail.time = 0;

        struct Node *node = (struct Node *)malloc(sizeof(struct Node));
        node->snail = snail;
        node->next = NULL;

        if (head == NULL)
        {
            head = node;
            p = node;
        }
        else
        {
            p->next = node;
            p = node;
        }
    }

    return head;
}

// 初始化顺序栈
void initStack(struct Stack *stack)
{
    stack->top = -1;
}

// 判断栈是否为空
int isStackEmpty(struct Stack *stack)
{
    return stack->top == -1;
}

// 判断栈是否已满
int isStackFull(struct Stack *stack)
{
    return stack->top == MAX_SIZE - 1;
}

// 入栈
void push(struct Stack *stack, int x)
{
    if (isStackFull(stack))
    {
        printf("栈已满\n");
        return;
    }
    stack->data[++stack->top] = x;
}

// 出栈
int pop(struct Stack *stack)
{
    if (isStackEmpty(stack))
    {
        printf("栈为空\n");
        return -1;
    }
    return stack->data[stack->top--];
}
printf("输入树的高度 h 和蜗牛的数量 n: ");
int h, n;
scanf("%d%d", &h, &n);

// 创建链表
struct Node *head = createList(n);

// 初始化顺序栈
struct Stack stack;
initStack(&stack);

// 循环模拟蜗牛的爬行过程
while (1)
{
    int flag = 1; // 标记是否还有蜗牛在爬行
    struct Node *p = head;
    while (p != NULL)
    {
        struct Snail snail = p->snail;
        snail.height += snail.ni;
        snail.height -= snail.mi;
        if (snail.height < 0)
        {
            snail.height = 0;
        }
        if (snail.height >= h)
        {
            snail.height = h;
            snail.time++;
        }
        else
        {
            flag = 0;
        }
        push(&stack, snail.height); // 压入栈中
        p->snail = snail;
        p = p->next;
    }
    if (flag)
    {
        break;
    }
}

// 统计树的每一米线都有多少次蜗牛爬过
int count[MAX_SIZE];
memset(count, 0, sizeof(count));
while (!isStackEmpty(&stack))
{
    int x = pop(&stack);
    count[x]++;
}

// 输出每一米线被爬过的次数
for (int i = 0; i <= h; i++)
{
    printf("第 %d 米线被爬过的次数: %d\n", i, count[i]);
}

// 找出哪只蜗牛爬得最快
int minTime = -1;
struct Node *p = head;
while (p != NULL)
{
    struct Snail snail = p->snail;
    if (minTime == -1 || snail.time < minTime)
    {
        minTime = snail.time;
    }
    p = p->next;
}

// 输出爬得最快的蜗牛的爬行速度规律
p = head;
while (p != NULL)
{
    struct Snail snail = p->snail;
    if (snail.time == minTime)
    {
        printf("爬得最快的蜗牛的爬行速度规律: ni=%d, mi=%d\n", snail.ni, snail.mi);
    }
    p = p->next;
}

return 0;
}

是按我之前发你的思路写的,可以对着看一下