手机怎么制作自己的网站,一般使用的分辨率的显示密度是(),怎么登录住建局官网,北龙中网 可信网站验证 费用青蛙跳台阶问题 —— (三种算法#xff09; 一.题目介绍1.1.题目1.2.图示 二.解题思路三.题解及其相关算法3.1.递归分治法3.2.动态规划算法#xff08;Dynamic Programming#xff09;3.3.斐波那契数列法 四.注意细节 一.题目介绍
1.1.题目 一只青蛙一次可以跳上1级台阶 一.题目介绍1.1.题目1.2.图示 二.解题思路三.题解及其相关算法3.1.递归分治法3.2.动态规划算法Dynamic Programming3.3.斐波那契数列法 四.注意细节 一.题目介绍
1.1.题目 一只青蛙一次可以跳上1级台阶也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 答案需要取模 1e971000000007如计算初始结果为1000000008请返回 1。 示例 1 输入n 2 输出2 示例 2 输入n 7 输出21 示例 3 输入n 0 输出1 提示 0 n 100 1.2.图示 二.解题思路 此类求多少种可能性 的题目一般都有递推性质 即 f(n)和 f(n-1)…f(1)之间是有联系的。 设跳上 n级台阶有 f(n)种跳法。在所有跳法中青蛙的最后一步只有两种情况 跳上1级或 2级台阶。 当为 1级台阶 剩 n-1个台阶此情况共有 f(n-1)种跳法 当为 2级台阶 剩 n-2个台阶此情况共有 f(n-2)种跳法。 f(n)为以上两种情况之和即 f(n)f(n-1)f(n-2)以上递推性质为斐波那契数列。本题可转化为求斐波那契数列第 n项的值 与斐波那契数列等价唯一的不同在于起始数字不同。 青蛙跳台阶问题 f(0)1 , f(1)1, f(2)2 斐波那契数列问题 f(0)0 , f(1)1, f(2)1。 三.题解及其相关算法 斐波那契数列的定义是 f(n 1) f(n) f(n - 1)生成第n项的做法有以下几种 3.1.递归分治法 递归分治法 原理 把 f(n)问题的计算拆分成 f(n-1)和 f(n-2)两个子问题的计算并递归以 f(0)和 f(1)为终止条件。 缺点 大量重复的递归计算例如 f(n)和 f(n - 1)两者向下递归都需要计算 f(n - 2)的值。 这个程序的时间复杂度为 O(2^n)因为我们需要递归地计算从 1 到 n 的所有整数的和。在输入的楼梯数较大时程序可能会运行超时。 #include stdio.hint climbStairs(int n) {int con(int)1e9 7;if (n 1) {return 1;}else if (n 2) {return 2;}else {return climbStairs(n - 1)%con climbStairs(n - 2)%con;}
}int main() {int n;printf(请输入楼梯的阶数);scanf(%d, n);int ways climbStairs(n);printf(%d 阶楼梯一共有 %d 种跳法。\n, n, ways);return 0;
} 3.2.动态规划算法Dynamic Programming 动态规划算法Dynamic Programming记忆化递归法 动态规划 是一种用于解决多阶段决策问题的算法它通过将问题分解为更小的子问题并通过存储已经解决的子问题的结果来避免重复计算。 原理 在递归法的基础上新建一个长度为 n的数组用于在递归时存储 f(0)至 f(n)的数字值重复遇到某数字时则直接从数组取用避免了重复的递归计算。 缺点 记忆化存储的数组需要使用 O(N)的额外空间。 #define MAX 100
int ClimbStairs(int number)
{int con (int)1e9 7;if (number 1)return 1;else if (number 2)return 2;else{int dp[MAX];dp[1] 1;dp[2] 2;int i 0;for (i 3; i number; i){dp[i] dp[i - 1] % con dp[i - 2] % con;}return dp[number];}
}int main() {int n;printf(请输入楼梯的阶数);scanf(%d, n);int ways climbStairs(n);printf(%d 阶楼梯一共有 %d 种跳法。\n, n, ways);return 0;
}3.3.斐波那契数列法 斐波那契数列法 原理 以斐波那契数列性质 f(n 1) f(n) f(n - 1)为转移方程。 从计算效率、空间复杂度上看斐波那契数列法是本题的最佳解法。 int fbnq(int n)
{int con (int)1e9 7;int first 0;int second 1;int tem 0;while (n--){tem first second;first second % con;second tem % con;}return first;
}
int ClimbStairs(int n) {return fbnq(n 1);
}
int main() {int n;printf(请输入楼梯的阶数);scanf(%d, n);int ways ClimbStairs(n);printf(%d 阶楼梯一共有 %d 种跳法。\n, n, ways);return 0;
}四.注意细节 为什么要模1000000007。 参考https://link.zhihu.com/?targethttps%3A//www.liuchuo.net/archives/645 大数相乘大数的排列组合等为什么要取模 一、1000000007是一个质数素数对质数取余能最大程度避免结果冲突/重复 二、int32位的最大值为2147483647所以对于int32位来说1000000007足够大。int64位的最大值为2^63-1用最大值模1000000007的结果求平方不会在int64中溢出。 所以在大数相乘问题中因为(a∗b)%c((a%c)∗(b%c))%c所以相乘时两边都对1000000007取模再保存在int64里面不会溢出。 这道题为什么要取模取模前后的值不就变了吗 确实取模前 f(43) 701408733, f(44) 1134903170, f(45) 1836311903, 但是 f(46) 2147483647结果就溢出了。 取模后 f(43) 701408733, f(44) 134903163 , f(45) 836311896, f(46) 971215059没有溢出。取模之后能够计算更多的情况如 f(46)。这道题的测试答案与取模后的结果一致。 总结一下这道题要模1000000007的根本原因是标准答案取模了1000000007。不过大数情况下为了防止溢出模1000000007是通用做法原因见第一点。
文章转载自: http://www.morning.tknqr.cn.gov.cn.tknqr.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.tnjff.cn.gov.cn.tnjff.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.mpbgy.cn.gov.cn.mpbgy.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.fwnyz.cn.gov.cn.fwnyz.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.tqbw.cn.gov.cn.tqbw.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.khcpx.cn.gov.cn.khcpx.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.rqhdt.cn.gov.cn.rqhdt.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.ndltr.cn.gov.cn.ndltr.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.lkfhk.cn.gov.cn.lkfhk.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.lveyue.com.gov.cn.lveyue.com