首页 > 其他分享 >5th

5th

时间:2023-04-17 19:37:10浏览次数:25  
标签:return int 5th 兔子 斐波 fun

浅谈递归

经典问题 斐波那契数列

1 1 2 3 5 8 13 21 .....

ai=a(i-1)+a(i-2);

其实递归问题都可以想象成树状图

 

 

#include <iostream>
using namespace std;
int fun(int i);
//古典的斐波那契数列
//兔子 每三个月生一对,并假设所有兔子不死
/* 下面列举每个月的兔子数量
1 1 2 3 5 8 13 ......
可找出规律 第一二个数都为一 ,此后 A(i)= A(i-1)+ A(i-2)*/
int main(){
int c;
cin>>c;
cout<<fun(c);
}
int fun(int i){
if(i==1||i==2){
return 1;
}
else
return fun(i-1)+fun(i-2);
}

标签:return,int,5th,兔子,斐波,fun
From: https://www.cnblogs.com/wcy1111/p/17327176.html

相关文章