


#include <iostream>
#include <cstdio>
using namespace std;
//英语 抄博友程序 模拟 枚举
int main()
{
while(1)
{
int nut;
cin>>nut;
if(nut<0)
{
break;
}
int peo;
int flag=0;
for(peo=nut-1;peo>=2;peo--)
{
int sum=nut;
int step=0;
for(step=1;step<=peo;step++)
{
if((sum-1)%peo!=0)
{
break;
}
sum=sum-1-(sum-1)/peo;
}
if(step>peo && (sum)%peo==0)//抄博友程序
{
flag=1;
break;
}
}
if(flag==1)
{
printf("%d coconuts, %d people and 1 monkey\n",nut,peo);
}else
{
printf("%d coconuts, no solution\n",nut);
}
}
return 0;
}

本文通过C++程序模拟了一个经典的椰子分配问题:一群人在分配一定数量的椰子时,每人分得相同数量且剩余一个给猴子的情景。程序采用枚举的方法找到符合条件的人数。
370

被折叠的 条评论
为什么被折叠?



