How to solve the problem of chicken and rabbit cage with Python _python

Source: Internet
Author: User

This article describes the Python solution to chicken and rabbit cage problem with the method, share for everyone to reference. The specific analysis is as follows:

Problem description

A cage with chicken and rabbit (chicken has 2 feet, rabbit has 4 feet, no exception). Already know the total number of feet in the cage A, asked how many animals in the cage, at least how many animals

Enter data

The 1th row is the number of groups n that test data, followed by the input of n rows. Each set of test data occupies 1 rows, including a positive integer a (a < 32768).

Output requirements

n rows, each output corresponds to one input. The output is two positive integers, the first is the minimum number of animals, the second is the maximum number of animals, and two positive integers are separated by spaces. If the requirement is not met, the output is 2 0.

Input sample

2
3
20
Output sample
0 0
5 10

Ideas for solving problems:

First consider the special case, when a is odd, the result is 0
Second, consider even:
Suppose the chicken I only, the rabbit j only, then A=2*i+4*j
We require the minimum and maximum value of the I+j
Easy to know I+j= (A-2J)/2 when J bigger i+j smaller when J smaller i+j bigger
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 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

I hope this article will help you with your 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.