题目: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. HDU 1398 Square Coins 整数拆分变形 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  2. Storm之tickTuple

    tickTuple是Storm中引入的一种定时机制,利用tickTuple能够实现间隔一段时间进行某种处理的逻辑. 在boltA中实现tickTuple注册的方法如下 @Override public ...

  3. 【bzoj2330】[SCOI2011]糖果 差分约束系统

    题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配 ...

  4. [UVA1402]Robotic Sort;[SP2059]CERC07S - Robotic Sort([洛谷P3165][CQOI2014]排序机械臂;[洛谷P4402][Cerc2007]robotic sort 机械排序)

    题目大意:一串数字,使用如下方式排序: 先找到最小的数的位置$P_1$,将区间$[1,P_1]$反转,再找到第二小的数的位置$P_2$,将区间$[2,P_2]$反转,知道排序完成.输出每次操作的$P_ ...

  5. 【ZJ选讲·字符串折叠】

    给一个字符串(len<=100) 把这个字符串折叠(就是压缩) 记 X(子串) 表示重复 X次该子串 比如 3(orz)  orzorzorz  来点神奇例子: AAAAAAAAAA ...

  6. yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY

    yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY 博客分类: linux   三种解决方案 我采取第三种方案解决的 第一种: linu ...

  7. innodb_stats_on_metadata and slow queries on INFORMATION_SCHEMA

    INFORMATION_SCHEMA is usually the place to go when you want to get facts about a system (how many ta ...

  8. Codeforces Round #350 (Div. 2) B

    B. Game of Robots time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. 怎么给word加底纹

  10. SpringMVC学习 -- IDEA 创建 HelloWorld

    SpringMVC 概述: Spring 为展现层提供的基于 MVC 实际理念的优秀 Web 框架 , 是目前最主流的 MVC 框架之一. 自 Spring3.0 发布及 2013 年 Struts ...