用c语言表示:
发布网友
发布时间:2022-04-29 00:28
我来回答
共3个回答
热心网友
时间:2023-10-19 14:57
可以用穷举法,因为可能有2种以上的可能所以用了2个循环,第一个循环用来确认答案是存在的,第二个循环用来确认所有可能
#include<stdio.h>
void main()
{
int head,leg;
int chilk,rabbit;
printf("please input the count of head and leg\n");
scanf("%d%d",&head,&leg);
for(chilk=0,rabbit=head;chilk<=head+1;chilk++,rabbit--)
{
if(2*chilk+4*rabbit==leg)
{
printf("the count meybe:chilk:%d,rabbit:%d\n",chilk,rabbit);
break;
}
if(chilk==head+1)
printf("the count you have put meybe wrong,try input again!\n");
}
for(chilk=chilk+1,rabbit=rabbit+1;chilk<=head;chilk++,rabbit--)
if(2*chilk+4*rabbit==leg)
printf("the count meybe:chilk:%d,rabbit:%d\n",chilk,rabbit);
}
热心网友
时间:2023-10-19 14:57
main()
{
int ji,tou,jiao,sign;
sign=0;
scanf("%d%d",&tou,&jiao);
for(ji=1;ji<=tou;ji++)
if((ji*2+(tou-ji)*4)==jiao)
{printf("ji:%d,tu:%d\n",ji,(tou-ji));
sign=1;
break;
}
if(sign==0) printf("NO ANSWER\n");
}
热心网友
时间:2023-10-19 14:58
/* 就是解一个2元1次方程组,只有一组解,若为负数或小数则不输出 */
#include<stdio.h>
main()
{int i,j,head,foot;
laber:;
puts("Input head & foot:");
scanf("%d%d",&head,&foot);
for(i=0;i<=head;i++)
{j=head-i;
if((i*4+j*2)==foot)
{printf("Chinken are %d,rabbits are %d",j,i);
getch();
exit(0);
}
}
printf("Error nums,input again!\n");
goto laber;
}