In Africa there is a very special species of bee. Every year, the female bees of such species give birth
to one male bee, while the male bees give birth to one male bee and one female bee, and then they die!Now scientists have accidentally found one “magical female bee” of such special species to the effectthat she is immortal, but still able to give birth once a year as all the other female bees. The scientistswould like to know how many bees there will be after N years. Please write a program that helps themfind the number of male bees and the total number of all bees after N years.InputEach line of input contains an integer N (≥ 0). Input ends with a case where N = −1. (This caseshould NOT be processed.)OutputEach line of output should have two numbers, the first one being the number of male bees after Nyears, and the second one being the total number of bees after N years. (The two numbers will notexceed 232.)Sample Input13-1Sample Output1 24 7
题意: 蜜蜂,每年每只雄蜂产下一只雌蜂和一只雄蜂,每只雌蜂产下一只雄蜂,然后就死去, 现在发现了一只不会死的雌蜂,问以她为起始点,第N年有多少雄蜂和一共多少蜜蜂。
题解: 设第i年的雄蜂和雌蜂分别为a(i)与s(i),则有如下递推关系:
1 s(i)= a(i-1)+ 1
2 a(i)= s(i-1)+ a(i-1)
整理得:a(i)=a(i-1)+ a(i-2)+ 1;
#include#include #include #include #include #include #include #include