题目: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的更多相关文章

  1. 【leetcode】Climbing Stairs

    题目简述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

  2. 【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 ...

  3. 【题解】【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 ...

  4. 【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 ...

  5. 【HDOJ】3088 WORM

    状态压缩+BFS. /* 3088 */ #include <iostream> #include <cstdio> #include <cstring> #inc ...

  6. 【hdu 4315】Climbing the Hill

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  7. 【目录】Leetcode

    Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...

  8. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  9. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

随机推荐

  1. TLS协议分析

    TLS协议分析 本文目标: 学习鉴赏TLS协议的设计,透彻理解原理和重点细节 跟进一下密码学应用领域的历史和进展 整理现代加密通信协议设计的一般思路 本文有门槛,读者需要对现代密码学有清晰而系统的理解 ...

  2. 算法(3)Rotate Array

    题目:将一个n个元素的数组右移k位,比如n=7,k=3,对数组[1,2,3,4,5,6,7]作如下旋转[5,6,7,1,2,3,4] 思路:[5,6,7,1,2,3,4],不知大家看出来了没有呢,两次 ...

  3. Java中动态代理实现原理深究

    一.前言 笔者平时开发使用“动态代理”不多,最近在看设计模式的时候,“动态代理”又在面前晃了几次,所以这次想从源码的角度去分析动态代理的实现原理,以窥探其精妙~ 二.正文 2.1 静态代理  本文源码 ...

  4. Bootstrap中的Affix插件

    我们为什么要用bootstrap?因为懒!哦....不,是因为方便,呃...意思差不多. 今天来说说Affix这个插件,它可以使导航栏固定,免去了自己手写的麻烦,用着非常方便,废话不多说,下面是用法. ...

  5. oracle或mysql定时增量更新索引数据到Elasticsearch

    利用kettle Spoon从oracle或mysql定时增量更新数据到Elasticsearch https://blog.csdn.net/jin110502116/article/details ...

  6. AOJ.602 大家来找茬

    大家来找茬 Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 627 Submiss ...

  7. springMvc--请求的跳转和传值

    springMvc--请求的跳转和传值 目录 forword跳转页面的三种方式 1.使用serlvet 2.使用Model对象 3.使用ModelAndView redirect跳转到页面 使用ser ...

  8. rsync 同步

    1./usr/bin/rsync -vzrtopg --progress  --include "weibo-service-server" --exclude "/*& ...

  9. java简单发送邮件

    需要的jar 据说是: <dependency> <groupId>javax.mail</groupId> <artifactId>mail</ ...

  10. 【Foreign】远行 [LCT]

    远行 Time Limit: 20 Sec  Memory Limit: 256 MB Description Input Output Sample Input 0 5 10 1 4 5 2 3 2 ...