您的位置:首页 > 其它

第39级台阶

2018-03-11 21:15 211 查看
【问题描述】

小明刚刚看完电影《第39级台阶》。离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!

站在台阶前,他突然又想着一个问题:

如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?

请你利用计算机的优势,帮助小明寻找答案。



此问题重点在于走的最后步数是偶数步,要完成39级台阶,每一步是1台阶或2台阶,可以用递归的方法求解:

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int count = 0;

void f(int num, int steps){
if(num<0) return;
if(num == 0){
if(steps % 2 == 0) count++;
return;
}
f(num-1, steps+1);
f(num-2, steps+1);
}

int main() {
f(39,0);
printf("%d", count);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 蓝桥