m0_73536983 2022-12-30 15:28 采纳率: 55.6%
浏览 30
已结题

蚂蚁爬树有错误求改正

    #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--];}
int main(){    
printf("-----------------------蜗牛爬树-----------------------\n");    
printf("***************1.输入树的高度和蜗牛的数量*************\n\n");
printf("**************2.输出每一米线被爬过的次数**************\n\n");    
printf("*************3.输出爬的最快的蜗牛的爬行规律***********\n");    
printf("------------------------------------------------------\n\n");    
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)      
  {           
 p->snail.height += p->snail.ni;        //爬行         
    p->snail.height -= p->snail.mi;        //滑行             p->snail.time++;         
   if (p->snail.height < 0)            //位置小于0,将位置设置为0           
  {                p->snail.height = 0;            }       
     if (p->snail.height >= h)            //位置大于h,将位置设置为h           
  {                p->snail.height = h;                break;            }            else          
  {                flag = 0;            }      
      push(&stack, p->snail.height); 
// 压入栈中            p = p->next;        }     
   if (flag)        {            break;        }    }    
// 统计树的每一米线都有多少次蜗牛爬过    
int count[MAX_SIZE];    
while (!isStackEmpty(&stack))    {        int x = pop(&stack);        count[x]++;    } 
    // 输出每一米线被爬过的次数
    for (int i = 0; i <= h; i++)    
{        printf("第 %d 米线被爬过的次数: %d\n\n", i, count[i]);    }
    printf("\n");
    // 找出哪只蜗牛爬得最快
    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\n", snail.ni, snail.mi);        }        
p = p->next;    }    return 0;}

  • 写回答

2条回答 默认 最新

  • |__WhoAmI__| 2022-12-30 15:32
    关注

    在这个代码中,蜗牛是按照输入的顺序进行模拟的。当一只蜗牛爬到树顶时,它会将自己的高度压入栈中,然后从栈中弹出下一只蜗牛的高度,并将其作为自己的高度。这样如果栈中没有足够多的蜗牛的高度,那么下一只蜗牛就会从栈中弹出 -1 或 0。

    举个例子,假设蜗牛的数量为 2,树的高度为 3。假设输入的蜗牛的爬行速度分别为 2 和 3,滑行速度分别为 1 和 2。第一只蜗牛会爬到高度 2,然后将自己的高度压入栈中。第二只蜗牛会从栈中弹出第一只蜗牛的高度 2,并将其作为自己的高度。然后第二只蜗牛会爬到高度 3,然后将自己的高度压入栈中。但此时栈中只有一个元素,所以第一只蜗牛会从栈中弹出 0,并将其作为自己的高度。

    因此在这个代码中,输出会出现负值和 0 的原因是,当栈中没有足够多的蜗牛的高度时,会从栈中弹出 -1 或 0。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月7日
  • 已采纳回答 12月30日
  • 修改了问题 12月30日
  • 修改了问题 12月30日
  • 展开全部

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持