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 ...
随机推荐
- Linux wpa_cli 调试方法
记录一下如何使用wpa_cli来进行wifi调试. 1.启动WLAN (1)加载驱动 打开wifi的时候会加载驱动,关闭则会卸载wifi驱动.手动调试的时候,先调用insmod/rmmod命令加载/ ...
- Azure Key Vault (1) 入门
<Windows Azure Platform 系列文章目录> 为什么要使用Azure Key Vault? 我们假设在微软云Azure上有1个场景,在Windows VM里面有1个.NE ...
- dubbo项目中包的依赖说明
依赖 (+) (#) 必需依赖 JDK1.5+ 理论上Dubbo可以只依赖JDK,不依赖于任何三方库运行,只需配置使用JDK相关实现策略. 缺省依赖 通过mvn dependency:tree > ...
- 技术胖Flutter第三季-15垂直布局Column组件
博客地址: https://jspang.com/post/flutter3.html#toc-8eb 垂直布局 左对齐: crossAxisAlignment: CrossAxisAlignment ...
- ASP.NET学习笔记(一)相关概念
ASP.NET 是一个开发框架,用于通过 HTML.CSS.JavaScript 以及服务器脚本来构建网页和网站. ASP.NET 支持三种开发模式: Web Pages MVC Web Forms ...
- 设置a 标签打开新窗口新姿势
设置页面中的所有a标签都打开新窗口 1,在写的时候就加上target="_blank" 2,在页头<head></head>里加上 <base tar ...
- 看后端程序员调试CORS的姿势
# 目录 为什么有同源策略? 需要解决的问题 CORS跨域请求方案 preflight withCredentials 附:高效.优雅地调试CORS实现 为什么有同源策略? 同源策略Sam ...
- 洛谷 - P1020 - 导弹拦截 - 最长上升子序列
https://www.luogu.org/problemnew/show/P1020 终于搞明白了.根据某定理,最少需要的防御系统的数量就是最长上升子序列的数量. 呵呵手写二分果然功能很多,想清楚自 ...
- Event事件的三个阶段
转自www.w3school.com.cn/htmldom/event_bubbles.asp 在 2 级 DOM标准中,事件传播分为三个阶段: 第一,捕获阶段.事件从 Document 对象沿着文档 ...
- cogs 1176. [郑州101中学] 月考
1176. [郑州101中学] 月考 ★ 输入文件:mtest.in 输出文件:mtest.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 在上次的月考中Bug ...