Analysis on the problem of caged chicken and rabbit

Source: Internet
Author: User

In solving the problem of chicken and rabbit cage, many people are directly calculated the number of chickens and the number of rabbits, and not to analyze whether the data given is a valid value. If not, then although the number of chickens and rabbits can be calculated, but these numbers do not make any sense, and at this time still have to find the standard to check whether the quantity is correct. Therefore, you must first determine whether the given value is a valid value. such as chicken and rabbit cage problem, suppose you give M leg, n head, let you calculate a chicken and B rabbit's exact value. The criteria for determining whether m and N are valid are as follows:

1, M must be an even number (m%2==0 established)

2, if these heads are chickens, then the number of legs calculated is less than or equal to the number of legs (2*a<=m established)

3, if these heads are rabbits, then the number of legs calculated is greater than or equal to the number of legs (2*b>=m established)

When the above 3 conditions are true, then the values of M and n are valid, then the values of a and B are calculated with the derivation of the correct valid value, otherwise (at least one of them is not valid when M and n is not the effective value) there is no answer.

Analysis on the problem of caged chicken and rabbit

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.