斐波那契数列:0,1,1,2,3,5,8,13,21,34……(后一个数总是前两个数之和)
python:
a=int(input())
def shulie(x):
if(x<=1):
return x;
else:
return shulie(x-1)+shulie(x-2)
print(shulie(a))
结果:
c++:
#include <stdio.h>
#include <stdlib.h>
int fei(int n){
if(n==1||n==2)
return 1;
else
return fei(n-1)+fei(n-2);
}
int main(){
int a;
a=fei(4);
printf("斐波那契数列:%d",a);
}