December 2022 C/C++ (level three) real test analysis#中国电子学院#National Youth Software Programming Level Examination

insert image description here

Question 1: Chicken and rabbit in the same cage

A chicken and a rabbit are kept in a cage (the chicken has 2 legs, the rabbit has 4 legs, no exceptions). Knowing the total number a of feet in the cage, ask how many animals there are at least and how many animals are there at most.
Time limit: 1000
Memory limit: 65536
Input
One line, a positive integer a (a < 32768).
Output
One line, containing two positive integers, the first is the minimum number of animals, the second is the maximum number of animals, and the two positive integers are separated by a space. If there is no answer that meets the requirements, output two 0s separated by a space.
Sample input
20
Sample output
5 10

This problem can be solved mathematically. We know that chickens have 2 legs and rabbits have 4 legs, and there are no exceptions. Suppose the number of animals in the cage is x, the number of chickens is y, and the number of rabbits is z.

According to the problem conditions, we can get the following two equations:

(1) 2y + 4z = a (the total number of feet is a)

(2) y + z = x (the total number of animals is x)

What we need to solve is the minimum and maximum number of animals, that is, to find the minimum and maximum values ​​of x.

In the case of the least number of animals, we assume that all animals are rabbits, and each animal has 4

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132381518