递归--20层楼梯每次可以走1步2步4步,有几种走法
2012-10-31 02:30
281 查看
#include <stdio.h> #include <stdlib.h> #define SIZE_STACK 100 int sum = 0; int final_floor = 20; int step[3] = {1,2,4}; int count = 0; int push_count = 0; int top = -1; int stack[SIZE_STACK]; void push(int n) { if(top + 1 == SIZE_STACK) { fprintf(stderr,"%s\n","Stack FUll!"); return; } stack[++top] = n; } int pop() { if(top == -1) { fprintf(stderr,"%s\n","Stack Empty!"); return -1; } return stack[top--]; } void disStack() { ++ count; for(int i = 0; i <= top; ++i) { printf("%d ",stack[i]); } printf("\n"); } void get_step() { if(sum == 20) { disStack(); return; } for(int i = 0; i < 3; ++i) { if(sum + step[i] <= 20) { push(step[i]); sum += step[i]; get_step(); pop(); sum -= step[i]; } } } int main() { get_step(); printf("\n%d ways to go to 20 stairs.\n",count); return 0; }
相关文章推荐
- Climbing Stairs 爬楼梯问题,每次可以走1或2步,爬上n层楼梯总方法 (变相fibonacci)
- 有一个100个台阶的阶梯,一次可以选择上一个台阶,或者两个台阶,最后上到楼梯顶有几种走法?
- 【面试题】N级台阶(比如100级),每次可走1步或者2步,求总共有多少种走法?
- java:递归:上楼梯每次只能一步或者两步,有多少走法
- N级台阶(比如100级),每次可走1步或者2步,求总共有多少种走法?
- 15级楼梯,一步最多三级,爬上楼梯可以有多少种走法实现(js递归实现)
- 使用递归函数,将一个整型数组倒序输出。(数组用了随机数,每次的数组都不同,只用了调用,遗憾没用递归。。可以把for循环换成递归)
- 一个人爬楼梯,楼梯共20层,一人一步可以走一层或两层,共多少种走法?
- 青蛙上台阶,可以一下跳1步,也可以一下跳2步,n层台阶所有跳法?
- 也说说有N阶楼梯每次可以选择下1阶、2阶、3阶一共有多少种下法的问题
- 每次走1到2步,有多少种方法可以到达长度为N的终点:
- 一共有20级楼梯,每次可以上1级或2级,登上第20级一共有多少种上法
- 继承有几种方式,分别是什么,想要实现继承可以使用哪些方法
- 在Struts2中,Action可以以几种不同的方式来工作
- 描述 有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法? 注:规定从一级到一级有0种走法。
- (hdu step 8.2.1)Brave Game(巴什博奕——有n个石子,没人每次可以从里面去[1,m]个,求最后是谁赢了。)
- tyvj 1088 给出长度为N的数列{A_i},每次可以从最左边或者最右边取走一个数,第i次取数得到的价值是i * A_j。求价值之和最大的取数方案。
- 返回多个不同类型参数的值(out)可变参数一维数组( params )把值从方法中带出(ref) 方法递归(方法自己调用自己与循坏类似注意return每次只能跳出一个方法))
- jQuery方法中具有破坏性操作几种方法汇总,弄明白这几种特殊的方法,再去看其他方法可以轻松许多
- 几种可以实现在不同设备动态正常显示表格数据的方式