C语言猴子分香蕉
来源:网络收集 点击: 时间:2024-08-31打开Dev-C++

写好头函数
#includestdio.h

写好主函数

声明五个变量,one,two,three,four,five是每只猴子分成5堆后剩余的个数 ,使用穷举法,计算总共可能的数量
int temp,count,one,two,three,four,five;
//使用穷举法,count为香蕉总数,一个一个去尝试
//当one,two,three,four,five满足题目要求便输出count
//假设数量最多只有100000个,输出的结果,取最小的正确结果
for(count=1;count=100000;count++)
{
temp=count;
one=temp%5;
temp=temp-temp/5-one;
two=temp%5;
temp=temp-temp/5-two;
three=temp%5;
temp=temp-temp/5-three;
four=temp%5;
temp=temp-temp/5-four;
five=temp%5;
//若one,two,three,four,five满足题目要求便输出
if(one==1 two==2 three==3 four==4 five==0)
{
printf(%d\n,count);
}
}

总体代码
#includestdio.h
void main()
{
//one,two,three,four,five是每只猴子分成5堆后剩余的个数
int temp,count,one,two,three,four,five;
//使用穷举法,count为香蕉总数,一个一个去尝试
//当one,two,three,four,five满足题目要求便输出count
//假设数量最多只有100000个,输出的结果,取最小的正确结果
for(count=1;count=100000;count++)
{
temp=count;
one=temp%5;
temp=temp-temp/5-one;
two=temp%5;
temp=temp-temp/5-two;
three=temp%5;
temp=temp-temp/5-three;
four=temp%5;
temp=temp-temp/5-four;
five=temp%5;
//若one,two,three,four,five满足题目要求便输出
if(one==1 two==2 three==3 four==4 five==0)
{
printf(%d\n,count);
}
}
}

结果如下图

文件的格式一定要保存为.c
版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_1178601.html