Python solves the problem of coddle cage, and python solves the problem of coddle cage.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.