【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 =============================以下是最小生成树+并 ...
随机推荐
- hadoop worldcount小程序
首先在hadoop中建立input文件夹放几个文件,里边写点东西.比如我放了三个,分别写的是 第一个 hello hadoop bye hadoop 第二个 hello world bye world ...
- 【EasyNetQ】- 发布确认
默认的AMQP发布不是事务性的,并不保证您的消息实际到达代理.AMQP确实指定了事务发布,但是使用RabbitMQ它非常慢,我们还没有通过EasyNetQ API支持它.对于高性能保证交付,建议您使用 ...
- 2018牛客多校第一场 A.Monotonic Matrix
题意: 给一个n*m的矩阵赋值(0,1,2).使得每个数都不小于它左面和上面的数. 题解: 构建0和1的轮廓线.对于单独的轮廓线,共需要往上走n步,往右走m步.有C(n+m,n)种方式. 两个轮廓线的 ...
- BZOJ3573 [Hnoi2014]米特运输 【贪心】
题目链接 BZOJ3573 题解 题目又臭又长系列 题意:修改尽量少的点权,使得: ①同个节点的所有儿子点权相同 ②任意非叶节点权值等于其儿子权值之和 容易发现一旦任意一个点权值确定,整棵树权值就确定 ...
- 《软件调试的艺术》学习笔记——GDB使用技巧摘要
<软件调试的艺术>学习笔记——GDB使用技巧摘要 <软件调试的艺术>,因为名是The Art of Debugging with GDB, DDD, and Eclipse. ...
- BZOJ day2_plus
大半夜的刷b站,好爽啊... 突破十九题 1008105110591088117911911192143218761951196821402242243824562463276128184720
- 【NOIP模拟赛】 permutation 数学(打表)
biubiu~~~ 这道题卡读题卡得很死......首先他告诉我们读循环的时候要顺着圈读,然后又说这个圈在数列上要以最大数开始读,而且以这样的循环的首数排序,得到的序列与原序列一样那么他就是可行序列, ...
- 【BZOJ1458】士兵占领 最大流的模板题
我们只要把他们可以有的限制用流量限制,再用两者关系限制一下就可以开心的跑了. #include <cstdio> #include <cstring> #include < ...
- 禁止 iphone 网页上下拖动露底
document.addEventListener('touchmove', function(e) { e.preventDefault();});
- python 一些乱七八糟的东西
import random import os import sys import re class _is: def __init__(self,reg): self.cr=re.compile(r ...