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. 关于request对象的parameter和attribute

    request对象的parameter相关method用于浏览器和服务之间传递数据,且是单向的,只能由浏览器写数据,request读数据,所以只有 String getParameter(String ...

  2. 【移动端debug-4】iOS下setTimeout无法触发focus事件的解决方案

    开篇总结:其实目前无法解决这个bug. 这两天做项目遇到了这个case,项目需求是打开页面的时候,input元素自动弹起键盘.由于各种方面的考虑,我们希望通过setTimeout延时200毫秒让inp ...

  3. C# QR二维码DEMO

    QR二维码 二维码的一种 相关类库 ThoughtWorks.QRCode 第三方类库 DEMO功能 Encode 生成二维码图片 Encoding 编码 Correction Level 等级 Ve ...

  4. POJ3041_Asteroids

    这个题目说,有一个N*N的规格的方格.某些格子里有*号,每次可以消除一行或者一列中所有的*号.最少需要消多少次? 新学到的,什么什么定理,最少点覆盖等于最大匹配数. 这个定理可以这样来理解(看别人的) ...

  5. Wow! Such Doge! HDU - 4847(虽然是道水题 但很有必要记一下)

    就是求出现了多少次doge 不区分大小写  巧用字符串函数 isalpha 判断是否是字母 tolower 转换为小写字母 toupper 转换为大写字母 strncmp字符串比较函数  能限制比较的 ...

  6. 【刷题】BZOJ 2190 [SDOI2008]仪仗队

    Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是 ...

  7. 洛谷 P2731 骑马修栅栏 Riding the Fences 解题报告

    P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...

  8. 解题:洛谷2633 Count on a tree

    题面 在树上建主席树...... 每个点从父亲那里建过来,最后建出来就是从根到$i$这条链上的主席树,查询的时候一边差分一边查询 ($cmt[u]+cmt[v]-cmt[lca(u,v)]-cmt[a ...

  9. bzoj 3853 : GCD Array

    搬运题解Claris:1 n d v相当于给$a[x]+=v[\gcd(x,n)=d]$ $\begin{eqnarray*}&&v[\gcd(x,n)=d]\\&=& ...

  10. Centos 7安装Python3.6

    1> 安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel ...