Python solves the problem of coddle cage, and python solves the problem of coddle cage.
The example in this article describes how to solve the problem of coddle cage in Python and share it with you for your reference. The specific analysis is as follows:
Problem description
The chicken and rabbit are closed in a cage (the chicken has two feet and the rabbit has four feet, no exception ). I already know the total number of feet in the cage. I asked how many animals are there and how many animals are there at most.
Input data
The first row is the number of n groups of test data, followed by n rows of input. Each group of test data occupies one row, including a positive integer a (a <32768 ).
Output requirements
N rows, each row of output corresponds to an input. The output is two positive integers. The first is the minimum number of animals, and the second is the maximum number of animals. The two positive integers are separated by spaces. If the request is not met, two zeros are output.
Input example
2
3
20
Output example
0 0
5 10
Solution:
In special cases, when a is an odd number, the result is 0.
Next, consider the even number:
Suppose I am a chicken and j a rabbit, then a = 2 * I + 4 * j
We need the minimum and maximum values of I + j.
Yi Zhi I + j = (a-2j)/2 when j is larger I + j is smaller when j is smaller I + j is larger
Of course, we can also simply use the weight value to think about it, without having to think about a specific computing process.
Python implementation is as follows:
Copy codeThe Code is 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
I hope this article will help you with Python programming.