1253: 【入门】统计每个月兔子的总数

内存限制:16 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:26 解决:16

题目描述

有一对兔子,从出生后第3个月起每个月都生一对兔子,一对小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第n个月(n<=50)的兔子总数为多少对?

输入

输入1个整数n,表示第几个月

输出

第n个月兔子的总数量有多少?

样例输入 复制

9

样例输出 复制

34

提示


使用递归:

#include<bits/stdc++.h>
using namespace std;
int f(int x) {
	if (x<=2) 
	    return 1;
	else
	    return f(x-1)+f(x-2);
} 
int main(){
    int n;
    cin>>n;
    cout<<f(n);
	return 0;
}