【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 =============================以下是最小生成树+并 ...
随机推荐
- Elicpse新建userLibrary导入jar包时抛出NotFoundException异常
最近刚开始学Struts2.0框架.再导入jar包的时候突然心血来潮.为方便区分jar包的功能,于是想在WEB-INF -> lib 目录下想新建一个struts文件夹,以方便分类查看. 像上图 ...
- 基于JWT的无状态分布式授权【本文摘自智车芯官网】
简介 JWT是一种用于HTTP交互双方之间传递安全信息的简洁的.安全的表述性声明规范.JWT作为一个开发的标准,它定义了一种简洁的,自包含的方法用于通信双发之间以JSON形式安全传递.且因为数字证书的 ...
- 集显也能硬件编码:Intel SDK && 各种音视频编解码学习详解
http://blog.sina.com.cn/s/blog_4155bb1d0100soq9.html INTEL MEDIA SDK是INTEL推出的基于其内建显示核心的编解码技术,我们在播放高清 ...
- DataBase -- Customers Who Never Order
Question: Suppose that a website contains two tables, the Customers table and the Orders table. Writ ...
- BZOJ1558 [JSOI2009]等差数列 【线段树】
题目链接 BZOJ1558 题解 等差数列,当然是差分一下 差分值相同的连续位置形成等差数列,我们所选的两个等差数列之间可以有一个位置舍弃 例如: \(1 \; 2 \; 3 \; 6 \; 8 \; ...
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...
- bzoj 3456 城市规划 多项式求逆+分治FFT
城市规划 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1091 Solved: 629[Submit][Status][Discuss] Desc ...
- VC++使用CImage在内存中Bmp转换Jpeg图片
之前写了一篇<VC++使用CImage在内存中Jpeg转换Bmp图片>,通过CImage实现了在内存中Jpeg转Bmp. 既然Jpeg能转Bmp,那CImage也支持Bmp转Jpeg,与上 ...
- c++ 公有继承的赋值兼容规则
赋值兼容规则是指在需要基类对象的任何地方都可以使用公有派生类的对象来替代.通过公有继承,派生类得到了基类中除构造函数.析构函数之外的所有成员,而且所有成员的访问控制属性也和基类完全相同.这样,公有派生 ...
- spring aop与aspectj
AOP:面向切面编程 简介 AOP解决的问题:将核心业务代码与外围业务(日志记录.权限校验.异常处理.事务控制)代码分离出来,提高模块化,降低代码耦合度,使职责更单一. AOP应用场景: 日志记录.权 ...