Chickens and Rabbits (雞兔同籠)

  1. 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?
  2. Input: number of animals (a) & number of legs (leg).
  3. Output: number of chickens & number of rabbits.
  4. 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.
  5. Note: in this exercise, let us assume that the given input will always yield a valid solution.
    The program may run as follows. (Please leave a blank line between the input and the output so that it looks more clear.)
    
    Input the number of animals and the total number of legs, separated by spaces -- 4 10
    
    There are 3 chicken(s) and 1 rabbit(s).
    
    

    or
    
    Input the number of animals and the total number of legs, separated by spaces -- 11 34
    
    There are 5 chicken(s) and 6 rabbit(s).