当前位置:首页 » 编程语言 » 猴子分桃问题c语言

猴子分桃问题c语言

发布时间: 2023-01-01 00:39:22

❶ 猴子分桃问题,c语言,急~~~

答案是:至少剩8188,原来至少84371
/*猴子分桃改版*/
#include <stdio.h>

int main()
{
int previous (int current, int share, int remaining); /*定义分桃函数,current分完剩下数值,share份数,remaining被吃的余数,返回值为分前数目*/
int monkeyNum1=3, monkeyNum2=5; /*两组猴子数*/
int peachNum, peachNumr; /*桃子的总数和最后剩下的数目*/
int i,j; /*中间变量*/
for (i=1;i<10000;i++)
{
peachNum=i;
for(j=1;j<=monkeyNum2;j++)
{
peachNum=previous(peachNum, monkeyNum2, 1);
if(peachNum==0)
break;
}
if(peachNum==0)
continue;
for(j=1;j<=monkeyNum1;j++)
{
peachNum=previous(peachNum, monkeyNum1, 2);
if(peachNum==0)
break;
}
if(peachNum==0)
continue;
else
break;
}
peachNumr=i;
printf("At last remaining: %d\nNumber of all the peaches: %d\n",peachNumr,peachNum);
return 0;
}

int previous (int current, int share, int remaining)
{
int previousNum;
previousNum=current*share/(share-1)+remaining;
if(previousNum%share!=remaining)
return 0;
return previousNum;
}

❷ 用C语言编一程序 猴子分桃的问题

摘的数量最少是什么样的情况?我想应该满足:
1,第5只猴子有桃子吃;
2,第5只猴子在醒后吃桃子之前把桃子平均分成了5份,而且还多了一个桃子(隐含意义是第5只猴子吃了2个桃子,剩下4个桃子,这样才满足摘的数量最少);
那么我们就知道了,最后一个猴子(将猴子数量扩展为未知,猴子数为n)醒后还有n+1个桃子;
第n-1个猴子醒后还有n*(n+1)+1个桃子;
第n-2个猴子醒后还有n*(n*(n+1)+1)+1;
以此类推,可由递归算出桃子总数

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

#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-1;i++)
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=0;i<N;i++)
{
data[i].id = i+1;
data[i].tao = 0;
}
queue_init(&q);
for(i=0;i<N;i++)
queue_in(q, data[i]);

for(i=0;i<N;i++)
{
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;
}

❹ c语言:五猴分桃问题

4/5是整型运算,会导致结果为0
于是后续a一直是0
无法退出循环

热点内容
新建文件夹3在线 发布:2025-07-02 14:42:51 浏览:213
安卓手机微信默认浏览器怎么设置 发布:2025-07-02 14:14:55 浏览:503
数据库质检 发布:2025-07-02 14:13:41 浏览:458
opensslvc编译 发布:2025-07-02 14:13:31 浏览:885
linux三系统 发布:2025-07-02 14:13:30 浏览:39
华为云稳定服务器 发布:2025-07-02 13:58:09 浏览:428
安卓游戏在哪里下载免费 发布:2025-07-02 13:58:08 浏览:597
mts压缩 发布:2025-07-02 13:53:31 浏览:965
数据库的事务事务 发布:2025-07-02 13:51:15 浏览:610
买五菱s3哪个配置好 发布:2025-07-02 13:51:11 浏览:773