題目描述:
有一對兔子,從出生后第3個月起每個月都生一對兔子,一對小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少對?
輸入描述:輸入int型表示month
輸出描述:輸出兔子總數int型
輸入例子:9
輸出例子:34
算法實現:
#include<iostream> #include<string>#include<vector>using namespace std; //************************************************ // * Author: 趙志乾 // * Date: 2017-2-20 // * Declaration: All Rigths Reserved !!! //***********************************************/ int main() { int month; cin>>month; int a1=1,a2=1,a3=2; int ret; switch(month) { case 1: case 2: cout<<1<<endl;break; case 3: cout<<2<<endl;break; default: for(int i=4;i<=month;i++) { ret=a3+a2; a2=a3; a3=ret; } cout<<ret<<endl; } return 0; }
新聞熱點
疑難解答
圖片精選