1,斐波那契数列leetcode509
int fib(int n)
{
int fin;
if(n<=1)
return n;
if(n==2)
return n-1;
else
{
int a=1;
int b=1;
int c;
while(n-2)
{
c=a+b;
a=b;
b=c;
n--;
}
fin=c;
}
return fin;
}
2,leetcode1137,第n个泰波那契数
int tribonacci(int n)
{
int fin;
if(n<=1)
return n;
if(n==2)
return n-1;
else
{
int d;
int a=0;
int b=1;
int c=1;
while(n-2)
{
d=a+b+c;
a=b;
b=c;
c=d;
n--;
}
fin=d;
}
return fin;
}