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