Somethings about Floors题解
题目内容:一个楼梯有N级(N >=0), 每次走1级或2级, 从底走到顶一共有多少种走法?
输入要求:只有一行输入,并且只有一个数N(如果N > 20,则N = N%21,即保证N的范围控制为:0 <= N <= 20,当取模后的值为0时,输出1),代表这个楼梯的级数。
输出要求:只有一行,输出从底到顶的走法,后面有换行。
参考样例:
输入: 3
输出: 3
Hint:
问题分解。
分析:
我的代码:
#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题解的更多相关文章
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- CoderForces Round526 (A~E)题解
A. The Fair Nut and Elevator time limit per test 1 second memory limit per test 256 megabytes input ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
随机推荐
- OCX控件避免弹出安全警告的类
1.要加一个头文件: #include <objsafe.h>2.在控件头文件中加入: 1 DECLARE_INTERFACE_MAP()2 BEGIN_INTERFACE ...
- 安装ubuntu16.04之后无法关机和重启
这个问题是由于NVIDA驱动导致的,查询显卡型号,安装对应版本的驱动就可以解决此问题.ubuntu16.04 NVIDIA 驱动安装
- 解压缩zip,tar,tar.gz,tar.bz2文件
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gunz ...
- 屏蔽iframe中超链接
<style type="text/css"> .mask { position: absolute; width: 290px; height: 96px; z-in ...
- [hdu1277]全文检索(AC自动机)
解题关键:AC自动机模板题,注意字符匹配时若无法匹配,直接用%s即可. #include<bits/stdc++.h> using namespace std; typedef long ...
- SpringMVC数据绑定一(基本类型、数组和对象(简单对象、层级对象、多参数对象))
一.int和Integer类型 如:参数为int类型的请求 @Controller public class TestController { @RequestMapping(value=" ...
- 4 pyspark学习---RDD
开始新的东西,其实很多操作在第二篇的时候就有所介绍啦.在这里继续学习一遍加深一下印象. 1关于RDD (1) RDD-----Resilient Distributed Dataset,弹性分布式数据 ...
- C++多态性:虚函数的调用原理
多态性给我们带来了好处:多态使得我们可以通过基类的引用或指针来指明一个对象(包含其派生类的对象),当调用函数时可以自动判断调用的是哪个对象的函数. 一个函数说明为虚函数,表明在继承的类中重载这个函数时 ...
- https协议(4)
架构层次 HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTT ...
- F#周报2019年第20期
新闻 2019年理事会活动 "实用的F#挑战"意见截止日期接近,不要忘记提交博客文章或者其它作品 接口中的默认实现 .NET Core 3.0里的性能增强 使用Try .NET创建 ...