【hdoj_1049】Climbing Worm
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1049
以 上升-下降 一次为一个周期,一个周期时间为2分钟,每个周期上升距离为(u-d)。先只考虑上升,再只考虑下降。先上升n/u次,再下降n/u次,这样保证不会超过井口,这样上升和下降各n/u次之后离井口距离为 n-(u-d)*(n/u),用时n/u分钟。最后一定会出现这样的情况,离井口的距离<=u,这样,一次上升即可到达井口,用时1分钟(题目说明了不足一分钟按照一分钟算)。可以用递归和循环两种方式实现。
用Dev-C++编写的C++代码(提交AC)
<span style="font-size:18px;">#include <iostream>
#include <string.h>
using namespace std; int F(int n,int u,int d) //递归算法
{
if(u>=n) return 1;
else return 2*(n/u)+F(n-(u-d)*(n/u),u,d);
} int main()
{
int n,u,d,result[1000],k=0;
memset(result,0,sizeof(result));
while(1)
{
cin >> n >> u >> d;
if(n==0) break;
else //result[k++] = F(n,u,d); //递归算法
{
while(u<n) //非递归算法
{
int t = n / u;
result[k] += 2*t;
n = n - (u-d)*t;
}
result[k++] += 1;
}
}
for(int i=0;i<k;i++)
cout << result[i] << endl; return 0;
} </span>
注意:memset(a,0,sizeof(a))函数需要加载头文件 #include<string.h> 或 #include<memory.h>。
如有纰漏,恳请指正!
【hdoj_1049】Climbing Worm的更多相关文章
- 【leetcode】Climbing Stairs
题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- 【leetcode】Climbing Stairs (easy)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【题解】【DP】【Leetcode】Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【Leetcode】【Easy】Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【HDOJ】3088 WORM
状态压缩+BFS. /* 3088 */ #include <iostream> #include <cstdio> #include <cstring> #inc ...
- 【hdu 4315】Climbing the Hill
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【目录】Leetcode
Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
随机推荐
- 【EasyNetQ】- 快速开始
欢迎来到EasyNetQ.本指南向您展示如何在大约10分钟内启动并运行EasyNetQ. 你可以在GitHub上找到这个快速入门的代码:https://github.com/mikehadlow/Ea ...
- Bootstrap中轮播图
Bootstrap中轮播图插件叫作Carousel,为了清晰的表明每个标签在这里是什么意思,我把解释写在了下面的代码中. <!-- 以下容器就是整个轮播图组件的整体, 注意该盒子必须加上 cla ...
- Redis使用手册
简介 Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型. Key-Value数据库. Redis面向互联网的方案提供了三种形式: 1.主从 主机进行写操作, ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- BZOJ1076 [SCOI2008]奖励关 【状压dp + 数学期望】
1076: [SCOI2008]奖励关 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3074 Solved: 1599 [Submit][Sta ...
- Tomcat-Jdbc-Pool连接池参数说明
原文 http://liuxing.info/2016/01/05/Tomcat-Jdbc-Pool参数说明/ 转载收藏用,如有侵权,请联系删除,谢谢. 介绍 Tomcat 在 7.0 以前的版本都 ...
- git clone 出错 fatal: pack has bad object at offset 26060927: inflate returned -3
$ git clone http://xxx.xxx.cn/liyafei/developer.gitCloning into 'developer'...remote: Counting objec ...
- 移动端H5滚动穿透解决方案
最近遇到一个很 巨恶心的问题 ios10下面 页面弹窗有滚动穿透问题 各种google 终于找到了答案,但是体验还不是很好,基本能忍受 废话不多说,上方法 最后终于想到一个处理方案,就是第一种方案的 ...
- Codeforces Round #350 (Div. 2) C
C. Cinema time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Windows下安装Mycat
Mycat 首先在安装Mycat之前,需要安装JDK1.7以上,可以在cmd环境下输入 java -version 查看本地安装的java版本 如果未安装或者版本在1.7以下,请重新安装. 安装JDK ...