Climbing Stairs

本题收获:

1.斐波那契函数f(n) = f(n-1) + f(n -2)

  题目: 

  You are climbing a stair case. It takes n steps to reach to the top.

  Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

  有n阶台阶,每次可以走2步或者1步,问有多少种方法到达顶端

  思路:

    leetcode:是个斐波那契函数的迭代

      对于第n阶来说,有两种方法,从n-1 走 1阶 到n,  从n-2走2阶到n(刚开始想从n-2处到n 可以走1次2步  和  2次 1步,但是走1次一步不就成了n-1到n了, 重复)

  代码:

 class MyClass
{
public:
int clambingStairs(int n)
{
if (n == ) return ;
if (n == ) return ;
if (n == ) return ; int f2 = , f1 = , f = ;
for (int i = ; i < n; i++)
{
f = f1 + f2; //f2可以看做f(n-2)
f2 = f1; //f1看做f(n-1)
f1 = f; //f看做f(n)
}
return f;
}
};

  我的测试代码:

 // Climbing Stairs.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
using namespace std; class MyClass
{
public:
int clambingStairs(int n)
{
if (n == ) return ;
if (n == ) return ;
if (n == ) return ; int f2 = , f1 = , f = ;
for (int i = ; i < n; i++)
{
f = f1 + f2; //f2可以看做f(n-2)
f2 = f1; //f1看做f(n-1)
f1 = f; //f看做f(n)
}
return f;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
int n = ;
cin >> n;
MyClass solution;
int m = ;
m = solution.clambingStairs(n);
cout << m << endl;
system("pause");
return ;
}

2016.6.21——Climbing Stairs的更多相关文章

  1. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  4. FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM

    FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice t ...

  5. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  6. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  7. ”耐撕“团队 2016.3.21 站立会议3 2 1 GO!

    ”耐撕“团队 2016.3.21 站立会议 时间:2016.3.21  ① 17:20-17:45  ②17:55-18:10  总计40分钟 成员: Z 郑蕊 * 组长 (博客:http://www ...

  8. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  9. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

随机推荐

  1. <context-param> 标签引出的 web.xml 文件的加载顺序 [转]

    代码示例 : <context-param> <param-name>contextConfigLocation</param-name> <param-va ...

  2. String、StringBuffer与StringBuilder比较

    关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 1.三者在执行速度方面的比较:     StringBuilder > St ...

  3. AJAX实现无刷新登录

    最近学习了如何实现无刷新登录,大体的效果如下(界面比较丑,请自行忽略....): 点击登录按钮时弹出登录窗口,输入正确的用户名密码后点击登录则登录窗口关闭,状态改为当前用户名. 第一步: 首先弹出窗口 ...

  4. 【BZOJ1048】分割矩阵(记忆化搜索,动态规划)

    [BZOJ1048]分割矩阵(记忆化搜索,动态规划) 题面 BZOJ 洛谷 题解 一个很简单的\(dp\),写成记忆化搜索的形式的挺不错的. #include<iostream> #inc ...

  5. 【树论 1】 prim算法的学习和使用

    进阶版神犇可以看看本题解的姊妹篇 Kruskal算法的学习和使用 下面的内容是prim算法 但是最小生成树是什么呢? 标准定义如下:在边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值 ...

  6. USACO Section 1.5 Prime Palindromes 解题报告

    题目 题目描述 题目就是给定一个区间[a,b]((5 <= a < b <= 100,000,000)),我们需要找到这个区间内所有既是回文串又是素数的数字. 输入样例 5 500 ...

  7. 数字表格(product)

    Description Solution 一开始的时候我是这么推的(\(f(n)\)表示斐波那契数列的第\(n\)项) \[ \begin{aligned} Ans&=\prod_{x=1}^ ...

  8. Corosync+Pacemaker+crmsh构建Web高可用集群

    一.概述: 1.1 AIS和OpenAIS简介 AIS应用接口规范,是用来定义应用程序接口(API)的开放性规范的集合,这些应用程序作为中间件为应用服务提供一种开放.高移植性的程序接口.是在实现高可用 ...

  9. 跟踪分析Linux内核的启动过程--20135334赵阳林

    解决ubuntu下make menuconfig错误问题 http://blog.sina.com.cn/s/blog_726684020100r1oo.html 安装好相关的软件之后,键入make ...

  10. android:shape的使用(+圆角ListView)(转)

    http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6087.html Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape ...