Classical problem: there is a rabbit. Since 3rd months after birth, a rabbit is born every month. After the third month, a rabbit is born every month. If the Rabbit does not die, what is the total number of rabbits per month?

Source: Internet
Author: User

Question: classical question: there is a rabbit. From the first 3rd months after birth, a rabbit is born every month. After the third month, a rabbit is born every month. If the Rabbit does not die, what is the total number of rabbits per month?

Program Analysis:

The first month --------------- 1

The next month ----------------- 1

The third month --------------- 2

The fourth month --------------- 3

Fifth month ----------------- 5

The sixth month --------------- 8

The seventh month --------------- 13

......

We found that, starting from the third month, the number of rabbits in the first two months is the total number of rabbits in the third month. Therefore, we define an array of rabbit books for 24 months. Each element stores the total number of rabbits in one month.


# Include <stdio. h> int main () {int M [23]; int I; m [0] = m [1] = 1; for (I = 0; I <24; I ++) {if (I = 0 | I = 1) {printf ("Number of rabbits in the % d month: 1 pair \ n ", I + 1);} else {M [I] = m [I-1] + M [I-2]; printf ("Number of rabbits in the % d month is: % d to \ n ", I + 1, m [I]) ;}} return 0 ;}

Running result (compiling environment Mac xcode ):



No array storage required:

# Include <stdio. h> int main () {long F1, F2; int I; F1 = F2 = 1; for (I = 1; I <= 12; I ++) // It will take up to 24 months, and it will be boring if it is too much {printf ("% 12ld % 12ld", F1, F2); // The number of each month will not be together, there will be spaces in the middle, because it is a long integer, so LD 12ld is to take a total of 12 places if (I % 2 = 0) printf ("\ n"); F1 = F1 + F2; // This month equals to the sum of the previous two months F2 = F1 + F2; // This month equals to the sum of the previous two months} return 0 ;}

Running result (compiling environment Linux GCC ):



Use the iteration method to query the number of rabbits in a month

# Include <stdio. h> int fun (int n) {If (n = 1 | n = 2) return 1; else return fun (n-1) + fun (n-2 );} int main () {int mouth; printf ("the number of rabbits you need to query"); scanf ("% d", & mouth); fun (mouth ); printf ("Number of rabbits in the % d month: % d to \ n", Mouth, fun (mouth); Return 0 ;}

Running result (compiling environment Linux GCC ):



Iteration Methods:

If you enter 5, call fun (),

Then return fun (5-1) + fun (5-2)

Through Function Iteration, fun (4) = fun (3) + fun (2)

Fun (3) = fun (2) + fun (1)

Fun (2) = 1;

Fun (1) = 1;

Launched fun (3) = 2;

Fun (4) = 3;

Fun (5) = 5;

   




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.