This paper describes the method of Python to solve the problem of chicken and rabbit cage, and share it for everyone's reference. The specific analysis is as follows:
Problem description
A cage is closed with chickens and rabbits (chickens have 2 feet, rabbits have 4 feet, no exceptions). Already know the total number of feet inside the cage A, ask how many animals in the cage, at least how many animals
Input data
Line 1th is the number of groups of test data n, followed by n-line input. Each group of test data accounted for 1 rows, including a positive integer a (a < 32768).
Output requirements
n rows, one input per line of output. The output is two positive integers, the first is the minimum number of animals, the second is the largest number of animals, and two positive integers are separated by a space. If the condition does not meet the requirements, Output 2 0.
Input sample
2
3
20
Output sample
0 0
5 10
Problem Solving Ideas:
Consider the special case first, when a is odd, the result is 0.
Second, consider even numbers:
Suppose chicken I only, Rabbit J only, then A=2*i+4*j
We require the minimum and maximum values of the I+j
Easy to know I+j= (A-2J)/2 when J Greater i+j smaller when J smaller i+j greater
Of course, we can also simply use the weight value to think, and do not need to think about the specific calculation process
Python implementations are as follows:
Copy the Code code as follows:
N=input ()
L=list ()
for k in range (n):
Inputnum=input ()
#odd
If inputnum%2!=0:
Min=max=0
Else
#even
#min
J=inputnum/4
I=inputnum%4/2
Min=i+j
#max
Max=inputnum/2
l+=[(Min,max)]
For a, B in L:
Print A, b
Hopefully this article will help you with Python programming.