1:n的阶乘
long long fac(int n)
{
if (n < 0)
{
return -1;
}
if (n == 0 || n == 1)
{
return 1;
}
return n * fac(n - 1);
}
2:斐波那契数列
long long fib1(int n)
{
if (n < 1)
{
return -1;
}
if (n == 1 || n == 2)
{
return 1;
}
return fib1(n - 1) + fib1(n - 2);
}
long double fib2(int n)
{
long double temp;
if (n < 1)
{
return -1;
}
long long *a=new long double[n+1];
a[1]=a[2]=1;
for(int i=3;i<=n;i++)
{
a[i]=a[i-1]+a[i-2];
cout<<a[i]<<endl;
}
temp=a[n];
delete []a;
return temp;
}