C语言程序设计——猴子分桃

C语言程序设计——猴子分桃,第1张

#include <stdlib.h>

#include<stdio.h>

#define N 10

#define M 20

struct monkey

{

int id

int tao

}

struct queue

{

struct monkey content[N]

struct monkey* head

struct monkey* tail

int length

}

int queue_init(struct queue** q)

{

(*q) = (struct queue*)malloc(sizeof(struct queue))

(*q)->head = &(*q)->content[0]

(*q)->tail = &(*q)->content[0]

(*q)->length = 0

return 0

}

int queue_in(struct queue* q,struct monkey in)

{

if(q->length == 0)

{

*(q->tail) = in

q->length++

}

else

{

q->tail++

*(q->tail) = in

q->length++

}

return 0

}

int queue_out(struct queue* q, struct monkey* out)

{

int i = 0

*(out) = *(q->head)

for(i <q->length-1i++)

q->content[i] = q->content[i+1]

q->tail--

q->length--

return 0

}

//队启缺首到队尾

int queue_a(struct queue* q)

{

struct monkey temp

queue_out(q, &temp)

queue_in(q,temp)

return 0

}

int main()

{

struct monkey data[N]

struct queue* q = NULL

struct monkey temp

int i

int kuang = 0

for(i=0i<Ni++)

{

data[i].id = i+1

data[i].tao = 0

}

queue_init(&q)

for(i=0i<Ni++)

queue_in(q, data[i])

for(i=0i<Ni++)

{

printf("%d\t%d\n",q->content[i].id,q->content[i].tao)

}

printf("leng=%d\n",q->基磨length)

printf("head %d\t tail %d\搏旁斗n",q->head->id,q->tail->id)

i = 0

while(q->length != 0)

{

if(kuang == 0)

{

i++

kuang += i

}

//队首猴取桃

if(q->head->tao + kuang <M)

{

q->head->tao += kuang

kuang = 0

queue_a(q)

}

else

{

kuang -= M - q->head->tao

q->head->tao += M - q->head->tao

queue_out(q, &temp)

printf("%d\t%d\n",temp.id,temp.tao)

}

}

return 0

}

死循环了!粗悉

if(m-1>0&&(m-1)%5==0&&(m-1)/5!=0)//判断桃子能否合题分配

这句里,之老唤前 m=1,这里始终不能成立,if 不运行,然后就一直循环。。岩含乎。。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12493723.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-25
下一篇2025-08-25

发表评论

登录后才能评论

评论列表(0条)

    保存