腾讯面试题目:有一个50阶的楼梯,每次可以上一阶或者两阶,总共的方法有多少种。
算法应用:递归
#include<stdio.h>
#include<stdlib.h>
//int tencent(int n)
double tencent(int n) //由于递归50次计算时一个很大的值,为了防止溢出,将int改成double型。
{
if(n==1)
{
return 1.0;
}
else if(n==2)
{
return 2.0;
}
else
{
return tencent(n-1)+tencent(n-2);
}
}
void main()
{
printf("%f",tencent(50));
getchar();
}