前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

1、鸡兔同笼

1只鸡有1个头,2只脚,1只兔子有1个头,4只脚,若已知头的数量和脚的数量,求鸡和兔子各有多少?

1.1、解题思路

解题思路: 输入 头x 脚y ,输出鸡a 兔子b

​ a + b = x

​ a * 2 + b * 4 = y

1.2、算法

@Test
public void start() {
    int x = 2;
    int y = 6;
    int a, b;
    for (a = 0; a <= x; a++) {
        b = x - a;
        if (a * 2 + b * 4 == y) {
            System.out.println("鸡的数量为:" + a + ",兔的数量为:" + b);
            return;
        }
    }
    System.out.println("不存在该组合");
}

1.3、测试

鸡的数量为1兔的数量为1

ContactAuthor