题目内容:一个楼梯有N级(N >=0), 每次走1级或2级, 从底走到顶一共有多少种走法?

输入要求:只有一行输入,并且只有一个数N(如果N > 20,则N = N%21,即保证N的范围控制为:0 <= N <= 20,当取模后的值为0时,输出1),代表这个楼梯的级数。

输出要求:只有一行,输出从底到顶的走法,后面有换行。

参考样例:

    输入: 3

    输出: 3 


Hint:

问题分解。


分析:
这道题目应该用递归的思想去解决推导出公式。如果只有一级楼梯,那么走法只有一种;如果有两级楼梯,那么走法有两种;...如果有N(N > 2)级楼梯,因为一次可以上一级或者两级,那么我们可以考虑从N-1级和N-2级楼梯到N级的走法,那么F(N) = F(N-1) + F(N-2)。
 

我的代码:

#include <stdio.h>
int main() {
int f0, f1, a;
int b, n, i;
scanf("%d", &n);
n = n % ;
if (n == || n == ) {
printf("%d\n", );
return ;
}
f0 = ;
f1 = ;
for (i = ; i <= n; i++) {
a = f0 + f1;
f0 = f1;
f1 = a;
}
printf("%d\n", f1);
return ;
}

标答:

// from younglee
// solve the problem in two different way, with recursion and no recursion.
#include<stdio.h>
#include<string.h>
#define N 100 #define RECUR 1
#define MODE 0 int dealWithRecursion(int f);
int dealWithNoRecursion(int f);
// to save the result.
int arr[N]; int main(void) {
int floor;
scanf("%d", &floor);
floor %= ;
if (MODE == RECUR) {
printf("%d\n", dealWithRecursion(floor));
} else {
memset(arr, , sizeof(arr));
printf("%d\n", dealWithNoRecursion(floor));
}
return ;
} int dealWithRecursion(int f) {
if (f == || f == ) return ;
return (dealWithRecursion(f - ) + dealWithRecursion(f - ));
} int dealWithNoRecursion(int f) {
if (arr[f] != ) return arr[f];
int result;
if (f == || f == ) result = ;
else result = dealWithNoRecursion(f - ) + dealWithNoRecursion(f - );
arr[f] = result;
return result;
}

这里用了两种方法,递归与非递归。


Somethings about Floors题解的更多相关文章

  1. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  2. CoderForces Round526 (A~E)题解

    A. The Fair Nut and Elevator time limit per test 1 second memory limit per test 256 megabytes input ...

  3. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  4. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  5. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  6. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  7. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  8. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  9. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

随机推荐

  1. AndroidStudio自动弹出Documentation

    AndroidStudio自动弹出Documentation窗口 例如,在布局文件中添加 Button 标签 敲完 <Butotn 回车后就自动出现 Documentation窗口 那如何关闭自 ...

  2. 搭建Linux的VMware Workstation Pro

    搭建准备环境: 1.win10操作系统: 2.VMware Workstation Pro12: 下载地址   http://www.vmware.com/products/workstation/w ...

  3. Log4j1的使用与log4j.properties的配置

    这里介绍Log4j1(即Log4j 1.x版本),这里以普通的Java项目为例,用eclipse编写,项目结构如下图所示: 该项目主要包括: java文件:Log4jv1Util 以及 Log4jv1 ...

  4. EF Core的安装、EF Core与数据库结合

    一.新建一个.net core的MVC项目                         新建好项目后,不能像以前一样直接在新建项中添加ef, 需要用命令在添加ef的依赖      二.EF Cor ...

  5. JAVA基础--JAVA 集合框架(泛型、file类)16

    一.集合总结 集合:Collection体系.Map体系. Collection体系:单列集合的共性操作规则. List:列表,可以重复,有下标,拥有特有的迭代器ListIterator. Array ...

  6. 洛谷 - P3768 - 简单的数学题 - 欧拉函数 - 莫比乌斯反演

    https://www.luogu.org/problemnew/show/P3768 \(F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}ijgcd(i ...

  7. 洛谷 - P4997 - 不围棋 - 并查集 - 模拟

    https://www.luogu.org/problemnew/show/P4997 首先是改变气的定义,使得容易计算,这个很好理解. 然后使用并查集,因为要维护整个连通块的性质. 最后的难点在于, ...

  8. C++函数调用过程深入分析

    http://blog.csdn.net/dongtingzhizi/article/details/6680050 0. 引言 函数调用的过程实际上也就是一个中断的过程,那么C++中到底是怎样实现一 ...

  9. 什么是socket ??

    socket起源于Unix, 而Unix/Linux基本哲学之一就是"一切皆文件", 都可以用"打开open -> 读写write/read -> 关闭clo ...

  10. ADO学途 four day 数据库左右连接

    数据库的多表操作 数据库用于存放用户数据,用户数据库的数据又会有不同表来存放不同类型的数据,这这是就会产生多 张表来满足需求.列如,部门表有市场部,技术部,行政部等.,子表就有员工具体信息表用来存放员 ...