腾讯面试题:50个阶梯,你一次可以上一阶或两阶,走上去,共有多少种走法?

递归面试题

分析题目:

到第一层只有一种情况上一阶即可即为1

到第二层有两种情况11和2

到第三层有111、12、21三种情况

到第四层有1111、121、112、22、211吴种情况

...

要到第50层要么从48层上两阶到达或者从49层上一阶到达

f(50)=f(49)+f(48)

同理:f(49)=f(48)+f(47)

归纳得通式

f(n)=f(n-1)+f(n-2) (n>=2)

解决递归:

1.找出递归终止的条件

2.找出递归关系式

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<stdlib.h>
double calc_upstairs_recursion(int n) //值比较大所以使用double
{
    if(n==1)//找出递归终止条件
    {
        return 1;
    }
    else if(n ==2)
    {
        return 2;
    }
    else
    {
        return calc_upstairs_recursion(n-1)+calc_upstairs_recursion(n-2);//找出递归关系条件
    }
}
double calc_upstairs_loop(int n)
{
    double *tmp =NULL;
    double sum = 0;
    int i;
    if(n<=2)www.2cto.com
    {
        return n;
    }
    tmp =(double *)malloc(sizeof(double)*n);
    tmp[0]= 1;
    tmp[1] = 2;
    for( i=2;i<n;i++) pre="" return="" sum="tmp[n-1];" void="">2.给定个数组:int a[10]= {1,2,3,4,5,6,7,8,9,10};使用递归来判断数组a是否为递增序列?<p> </p><p> </p><p>1.找出递归终止的条件</p><p>2.找出递归关系式</p><pre class="brush:java;">#include<stdio.h>
#include<stdlib.h>
 
int is_increase(int start_pos,int length,int *pdata)
{
    if(start_pos==length-2) //递归结束的条件
    {
        return pdata[start_pos+1]>pdata[start_pos];
     
    }
 
    return pdata[start_pos+1]>pdata[start_pos]&&is_increase(start_pos+1,length,pdata);//递归关系
}
 
 
void main()
{
    int a[10]= {1,2,3,4,5,6,7,8,9,10};
    int res=is_increase(0,10,a);
    if(res ==1)
    {
        printf("是递增的\n");
    }
    else
    {
        printf("不是递增的\n");
    }
 
    system("pause");
}
}</stdlib.h></stdio.h></pre>
</n;i++)></stdlib.h></stdio.h><br>
<br>

相关文章: