PTA Programming Question (C Language)--Rabbit Reproduction Problem

Title: Rabbit Reproduction Problem Author: Weng Kai, Zhejiang University

It is known that a pair of rabbits can give birth to a pair of rabbits every month, and the baby rabbits can give birth to a pair of baby rabbits one month later (for example: a baby rabbit born in February can give birth to a baby in April). That is to say, the number of pairs of rabbits is: 1 pair in the first month, 2 pairs in the second month, 3 pairs in the third month, 5 pairs in the fourth month... Assume that the rabbit's reproductive period is two years , and will not die. So the question is, can you tell the number of rabbits every month?

Input format:

Enter a number n, representing the nth month, 1<=n<=24.

Output format:

Output the number of rabbits this month.

Input example:

4

Output sample:

5

Foreword:

First of all, this is a classic problem solved by iterative algorithms.

Secondly, the author of this question is Teacher Weng Kai, who has a high reputation in the C language teaching community. Many students do not understand this question very accurately. Below, I will explain the question as follows based on my personal understanding and evidence of the answer.

(1) "And a little rabbit can give birth to a pair of little rabbits in one month (for example: a little rabbit born in February can give birth in April)", the word "little rabbit" that appears for the first time in this sentence should refers to the pair of little rabbits born in that month; the meaning of "one month later", according to the explanation in brackets, should refer to the next month of the same month, that is, one month later; "another baby can be born" "Pair of little rabbits", according to the explanation in parentheses below, means that from the next month onwards, and every month thereafter, you can and can only give birth to a pair of little rabbits.

(2) The output format requires "output the number of rabbits this month." The number here refers to the total number of pairs of rabbits (not the number) in the Nth month.

(3) In fact, it should be explained: a pair of rabbits refers to a male rabbit and a female rabbit, and the resulting pair of rabbits is also a male and a female. Every time a pair gives birth to a pair.

The above only represents my personal views. Here are some ideas for solving the problem:

Idea:

First, we divided the rabbit pairs of each month into three categories: (1) rabbit pairs that are capable of reproduction in the current month, (2) rabbits that are not capable of reproduction in the current month, but will be capable of reproduction in the next month; (3) rabbits born in the current month rabbit. For these three types of rabbits, the intersection of pairs is empty, and the collection of three is the complete set.

Assume that the logarithms (or the number of rabbit pairs) of the three types of rabbits in the first month are a1, b1, and c1 respectively. Then the logarithms a2, b2, and c2 of the three types of rabbits in the second month should satisfy:

(i) a2 = a1+b1, // Those with the ability to reproduce this month = those that were there last month + those that were not available last month, but there will be those next to last month.

(ii) b2 = c1, // Those who are not capable of reproduction this month, but will be capable of reproduction next month = those who were just born last month.

(iii) c2 = a2 = a1+b1. // Newly born this month = those with the ability to reproduce this month.

By analogy, the logarithm of the three types of rabbits for each month and last month satisfies the above relationship.

Therefore, we only need to determine the values ​​​​of a1, b1, and a1 in the first month, and then use the above formula to iterate N-1 times to get the logarithm of the three types of rabbits in the Nth month.

Code:

#include <stdio.h>
int main () {
    int N, i, a = 1, b = 0, c = 0;
    scanf("%d", &N);
    for (i = 1; i < N; i++) {
        a = a+b;
        b = c;
        c = a;
    }
    printf("%d", a+b+c);
    return 0;
}

a, b, and c in the code respectively represent the logarithms of the three types of rabbits. When initialized, a=1, b=0, c=0; then iterate according to the formula every month, and the number of iterations is N-1; in the end, the total logarithm of the rabbit is a+b+c.

For more reference codes for PTA questions, you can search for "PTA question brushing assistant" in the wx applet.

おすすめ

転載: blog.csdn.net/morn_l/article/details/133955549