首先了解一下鸡兔同笼这个小学问题
已知设总头数h,总腿数f,鸡头数为chicken,兔头数为rabbit.
chicken+rabbit=h;
2*chicken+4*rabbit=f;
求解上面二元一次方程,得出:
鸡头数=(4*总头数–总腿数)/(4-2)
兔头数=(总腿数-2*兔头数)/(4-2)
程序
#include <stdio.h>; #include <stdlib.h> #include <string.h> int main() { int chicken, rabbit, foot, head; printf("input total heads:"); scanf("%d", &head); printf("total heads:\t%d ",head); printf("\ninput total feet:"); scanf("%d", &foot); printf("total feet:\t%d",foot); chicken = (4 * head - foot)/2; rabbit = (foot - 2 * head )/2; printf("\nchicken:%d\n", chicken); printf("rabbit:%d\n", rabbit); return 0; }
input total heads:3
total heads: 3
input total feet:8
total feet: 8
chicken:2
rabbit:1
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!