Menu Close

c语言写鸡兔同笼

首先了解一下鸡兔同笼这个小学问题

已知设总头数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
READ  C语言练习,检查一个整数是不是回文数
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in C 决策和循环语句, C语言习题集

Related Posts