Chickens and Rabbits (雞兔同籠)
-
Suppose you raise
chickens and rabbits together in a cage.
As we know, each chicken has two legs, while each rabbit has four legs.
If you only know the total number of animals, and the total number of
legs, can you write a program to calculate the exact number of chickens
and the number of rabbits?
- Input: number of animals (a) & number of legs (leg).
- Output: number of chickens & number of rabbits.
- You may derive a special formula to solve this problem, or you may
also re-use the program you developed in the homework of solving linear
equations.
- Note: in this exercise, let us assume
that the given input will
always yield a valid solution.