博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
每次可以走k步的爬楼梯种类数
阅读量:4217 次
发布时间:2019-05-26

本文共 1135 字,大约阅读时间需要 3 分钟。

Number of ways to reach Nth floor by taking at-most K leaps

Given N number of stairs. Also given the number of steps that one can cover at most in one leap (K). The task is to find the number of possible ways one (only consider combinations) can climb to the top of the building in K leaps or less from the ground floor.

Examples:

Input: N = 5, K = 3

Output: 5
To reach stair no-5 we can choose following combination of leaps:
1 1 1 1 1
1 1 1 2
1 2 2
1 1 3
2 3
Therefore the answer is 5.

Input: N = 29, K = 5

Output: 603

 

思路:先尝试1步的种类数,然后再尝试多步,直到k步,注意选择条件的循环因该放在外层。

#include
using namespace std;int get(int n, int k){ int dp[n+1]; memset(dp,0,sizeof(dp)); dp[0] = 1; for(int i = 1; i <= k; ++i){ for(int j = 0; j <= n; ++j){ if(j-i >= 0 ) dp[j] += dp[j-i]; } }// 错误 循环 选择循环 应放在外面// for(int i = 1; i <= n; ++i){// for(int j = 1; j <= k; ++j){// if(i-j >= 0)// dp[i] += dp[i-j];// }// } return dp[n];}int main() { int n,k; int t; scanf("%d%d",&n,&k); printf("%d\n",get(n,k)); return 0; }

 

转载地址:http://nwsmi.baihongyu.com/

你可能感兴趣的文章
资源监控工具 - Hyperic HQ
查看>>
LoadRunner中Concurrent与Simultaneous的区别
查看>>
SiteScope - Agentless监控
查看>>
为什么我们的自动化测试“要”这么难
查看>>
LoadRunner性能脚本开发实战训练
查看>>
测试之途,前途?钱途?图何?
查看>>
反病毒专家谈虚拟机技术 面临两大技术难题
查看>>
几种典型的反病毒技术:特征码技术、覆盖法技术等
查看>>
论文浅尝 | 通过共享表示和结构化预测进行事件和事件时序关系的联合抽取
查看>>
廖雪峰Python教程 学习笔记3 hello.py
查看>>
从内核看epoll的实现(基于5.9.9)
查看>>
python与正则表达式
查看>>
安装.Net Framework 4.7.2时出现“不受信任提供程序信任的根证书中终止”的解决方法
查看>>
input type=“button“与input type=“submit“的区别
查看>>
Linux文件和设备编程
查看>>
文件描述符
查看>>
终端驱动程序:几个简单例子
查看>>
HTML条件注释
查看>>
内核态与用户态
查看>>
趣链 BitXHub跨链平台 (4)跨链网关“初介绍”
查看>>