用c语言编程:鸡兔同笼,有三十五只头,九十四个脚,求鸡兔各有多少?
展开全部
#include <stdio.h>
void main()
{
int chicken, rabbit;
printf("鸡\t兔\n");
for (chicken = 0; chicken <= 35; chicken ++)
{
rabbit = 35 - chicken;
if (8 * rabbit + 2 * chicken == 94)
printf("%d\t%d\n", chicken, rabbit);
}
}
void main()
{
int chicken, rabbit;
printf("鸡\t兔\n");
for (chicken = 0; chicken <= 35; chicken ++)
{
rabbit = 35 - chicken;
if (8 * rabbit + 2 * chicken == 94)
printf("%d\t%d\n", chicken, rabbit);
}
}
追问
这是枚举法?
追答
是啊,不过略微改良了一下,其实只有一组解,完全意义上的枚举法如下:
#include
int main()
{
int chicken, rabbit;
printf("鸡\t兔\n");
for (chicken = 0; chicken <= 35; chicken ++)
{
for (rabbit =0; rabbit <= 35; rabbit ++)
{
if (rabbit + chicken == 35 && 4 * rabbit + 2 * chicken == 94)
printf("%d\t%d\n", chicken, rabbit);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询