A solution to a problem (rabbits are born every month after three months)

Source: Internet
Author: User

See a problem today, in fact, is an old problem, a whim, solved a bit, the problem is as follows:

There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the small rabbit to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask the total number of rabbits each month?

This question thought for one hours to understand, it seems that the level of intelligence generally, if you want to interview this problem, certainly not, so still write down.

The rabbit is divided into 3 categories, one can be born rabbits, a class is one months of birth, a class is born two months, so a simple chart is as follows:

month 0 1 2 3 4 5
can produce rabbits 1 1 1 2 3 4
1-month-old rabbit 0 1 1 1 2 3
2-month-old Bunny 0 0 1 1 1 2

You can see the following equation

The fertile rabbit of the month = Rabbit born last month + February-month-old rabbit

Rabbits of the January of the month = rabbits that can be born last month

Rabbit of the February of the month = the rabbit of the January month

That is, the rabbits that could have been born last month produced the same number of rabbits in the January, and the rabbits in January last month became the rabbits of February, and the rabbits in February last month became fertile rabbits.

So you can construct an array representation:

A[N+1][0]=A[N][0]+A[N][2]

A[N+1][1]=A[N][0]

A[N+2][2]=A[N][1]

where a[0][0]=1,a[0][1]=0,a[0][2]=0

It is easy to use recursive function to find the number of Rabbits Moon age, the total amount as long as added up can be, in any language is easy to implement, the following Java implementation:

 PackageRabbit;/** * * @authorFlysy*/ Public classRabbitnum { Public Static LongGetrabbitnum (intKinti) {if(k = = 0) && (i = = 0)) {            return1; }        if(k = = 0) && (i = = 1)) {            return0; }        if(k = = 0) && (i = = 2)) {            return0; }        if(i = = 0) {            returnGetrabbitnum (k-1, 0) + getrabbitnum (k-1, 2); }        if(i = = 1) {            returnGetrabbitnum (k-1, 0); }        if(i = = 2) {            returnGetrabbitnum (k-1, 1); }        return0; }     Public Static voidMain (string[] args) {System.out.println ("I\t3 month \t2 month \T1 month \ t Total");  for(inti = 0; I < 100; i++) {            LongS0 = Getrabbitnum (i, 0); LongS1 = Getrabbitnum (i, 1); LongS2 = Getrabbitnum (i, 2); Longsum = s0 + S1 +S2; System.out.println (i+ "\ T" + S0 + "\ T" + s1 + "\ T" + s2 + "\ T" +sum); }    }}

Because rabbits grow fast, they replace int with a long type, and the output is as follows
i    March    February    January    Total 0    1    0    0    1    1    0 1 1    1    (    2    1    1    )    3 2 1 each    4    3    2    6    4    3    137    9    6    4    198    9 6 289 9    4110    ,    6011,    8812    129 ...... ... ..........
The problem with this approach is that repetitive recursion is too powerful, and after outputting to 45 on my computer, it becomes very slow. So you need to optimize, you can save the intermediate variables, avoid repeating recursion, modify the program as follows:
/** To change the license header, choose License Headers in the Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ PackageRabbit;/** * * @authorFlysy*/ Public classRABBITNUM1 {Static LongA[][] =New Long[1000] [3];  Public Static LongGetrabbitnum (intKinti) {if(k = = 0) {            returnA[k][i]; } Else {             for(intm = 0; M < 3; m++) {                if(A[k-1][m] = = 0) {a[k-1][m] = Getrabbitnum (k-1, M); }            }            if(i = = 0) {                returnA[k-1][0] + a[k-1][2]; }            if(i = = 1) {                returnA[k-1][0]; }            if(i = = 2) {                returnA[k-1][1]; }            return0; }    }     Public Static voidMain (string[] args) {a[0][0]=1; System.out.println ("I\t3 month \t2 month \T1 month \ t Total");  for(inti = 0; I < 100; i++) {            LongS0 = Getrabbitnum (i, 0); LongS1 = Getrabbitnum (i, 1); LongS2 = Getrabbitnum (i, 2); Longsum = s0 + S1 +S2; System.out.println (i+ "\ T" + S0 + "\ T" + s1 + "\ T" + s2 + "\ T" +sum); }    } }

The result is an instant out, as follows:
I March February January total0 1 0 0 11 1 1 0 22 1 1 1 33 2 1 1 44 3 2 1 65 4 3 2 96 6 4 3 137 9 6 4 198 13 9 6 289 19 13 9 4110 28 19 13 601 1 41 28 19 8812 60 41 28 12913 88 60 41 18914 129 88 60 27715 189 12    9 88 40616 277 189 129 59517 406 277 189 87218 595 406 277 127819 872 595 406 187320 1278 872 595 274521 1873 1278 872 402322 2745 1873 1278 589623 402     3 2745 1873 864124 5896 4023 2745 1266425 8641 5896 4023 1856026 12664 8641 5896 2720127 18560 12664 8641 3986528 27201 18560 12664 5842529 39865 27201 18560 85626    30 58425 39865 27201 12549131 85626 58425 39865 18391632 125491 85626 58425 26954233 183916 125491 85626 39503334 269542 183916 125491 57894935 395033 269542 183916 84849136 578949 395033    269542 124352437 848491 578949 395033 182247338 1243524 848491 578949 267096439 1822473     1243524 848491 391448840 2670964 1822473 1243524 573696141 3914488 2670964 1822473 840792542 5736961 3914488 2670964 1232241343 8407925 5736961 3914488 1805937444 12322413 8407925 5    736961 2646729945 18059374 12322413 8407925 3878971246 26467299 18059374 12322413 5684908647     38789712 26467299 18059374 8331638548 56849086 38789712 26467299 12210609749 83316385 56849086 38789712 17895518350 122106097 83316385 56849086 26227156851 178955183 122106097 83316385 3    8437766552 262271568 178955183 122106097 56333284853 384377665 262271568 178955183 82560441654  563332848 384377665  262271568 120998208155 825604416 563332848 384377665 177331492956 1209982081 825604416 563332848 259891934557 1773314929 1209982081 825604416 380890142658 2598919345 1773314929 1209982081 558 221635559 3808901426 2598919345 1773314929 818113570060 5582216355 3808901426 2598919345 119900371 2661 8181135700 5582216355 3808901426 1757225348162 11990037126 8181135700 5582216355 257533891816 3 17572253481 11990037126 8181135700 3774342630764 25753389181 17572253481 11990037126 55315679788 65 37743426307 25753389181 17572253481 8106906896966 55315679788 37743426307 25753389181 118812495 27667 81069068969 55315679788 37743426307 17412817506468 118812495276 81069068969 55315679788 2551 9724403369 174128175064 118812495276 81069068969 37400973930970 255197244033 174128175064 11881249527 6 54813791437371 374009739309 255197244033 174128175064 80333515840672 548137914373 374009739309 255197244033 117734489     771573 803335158406 548137914373 374009739309 172548281208874 1177344897715 803335158406 548137914373     252881797049475 1725482812088 1177344897715 803335158406 370616286820976 2528817970494 1725482812088 1177344897715 543164568029777 3706162868209 2528817970494 1725482812088 796046365079178 543164568029 7 3706162868209 2528817970494 1166662651900079 7960463650791 5431645680297 3706162868209 170982721992 9780 11666626519000 7960463650791 5431645680297 2505873585008881 17098272199297 11666626519000 796046    3650791 3672536236908882 25058735850088 17098272199297 11666626519000 5382363456838583 36725362369088 25058735850088 17098272199297 7888237041847384 53823634568385 36725362369088 25058735850088 11560773278 756185 78882370418473    53823634568385 36725362369088 16943136735594686 115607732787561 78882370418473 53823634568385 248313 73777441987 169431367355946 115607732787561 78882370418473 36392147056198088 248313737774419 16943136735     5946 115607732787561 53335283791792689 363921470561980 248313737774419 169431367355946 78166657569234590 533352837917926 363921470561980 248313737774419 114558804625432591 781666575692345 533352837917926 3 63921470561980 167894088417225192 1145588046254325 781666575692345 533352837917926 246060745986459693 16 78940884172251 1145588046254325 781666575692345 360619550611892194 2460607459864596 1678940884172251 114 5588046254325 528513639029117295 3606195506118921 2460607459864596 1678940884172251 774574385015576896 5    285136390291172 3606195506118921 2460607459864596 1135193935627468997 7745743850155768 5285136390291172 3606195506118921 1663707574656586198 11351939356274689 7745743850155768 5285136390291172 2438281959672162999 16637075746565861 11351939356274689 7745743850155768 35734758952996318

A solution to a problem (rabbits are born every month after three months)

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.