YT14-先来练练手之爬动的蠕虫

Problem Description

An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting
then repeats. How long before the worm climbs out of the well? We‘ll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we‘ll assume the worm makes it out.

Input

There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end
of output.

Output

Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the well.

Sample Input

10 2 1
20 3 1
0 0 0

Sample Output

17
19

代码如下:

#include <iostream>
using namespace std;
int main()
{
    int n,u,d;
    while(cin>>n>>u>>d)
    {
        if(n==0&&u==0&&d==0)
            break;
        else
        {
            if(((n-u)%(u-d))==0)
                cout<<1+2*((n-u)/(u-d))<<endl;
            else
                cout<<1+2*(((n-u)/(u-d))+1)<<endl;
        }
    }
    return 0;
}  



时间: 2024-07-28 14:56:22

YT14-先来练练手之爬动的蠕虫的相关文章

循环-07. 爬动的蠕虫

循环-07. 爬动的蠕虫(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一条蠕虫长1寸,在一口深为N寸的井的底部.已知蠕虫每1分钟可以向上爬U寸,但必须休息1分钟才能接着往上爬.在休息的过程中,蠕虫又下滑了D寸.就这样,上爬和下滑重复进行.请问,蠕虫需要多长时间才能爬出井? 这里要求不足1分钟按1分钟计,并且假定只要在某次上爬过程中蠕虫的头部到达了井的顶部,那么蠕虫就完成任务了.初始时,蠕虫是趴在井底的(即高度为0). 输入格

循环-07. 爬动的蠕虫(15)

1 #include<iostream> 2 using namespace std; 3 int u,d; 4 int sum(int t){ 5 if(t%2==0) 6 return (u-d)*t/2; 7 else 8 return u*(t/2+1)-d*t/2; 9 } 10 int main(){ 11 int t,n; 12 cin>>n>>u>>d; 13 for(t=1;sum(t)<n;) 14 t++; 15 cout<

7-17 爬动的蠕虫(15 分)

一条蠕虫长1寸,在一口深为N寸的井的底部.已知蠕虫每1分钟可以向上爬U寸,但必须休息1分钟才能接着往上爬.在休息的过程中,蠕虫又下滑了D寸.就这样,上爬和下滑重复进行.请问,蠕虫需要多长时间才能爬出井? 这里要求不足1分钟按1分钟计,并且假定只要在某次上爬过程中蠕虫的头部到达了井的顶部,那么蠕虫就完成任务了.初始时,蠕虫是趴在井底的(即高度为0). 输入格式: 输入在一行中顺序给出3个正整数N.U.D,其中D<U,N不超过100. 输出格式: 在一行中输出蠕虫爬出井的时间,以分钟为单位. 输入样

PTA基础编程题目集——爬动的蠕虫

原题链接https://pintia.cn/problem-sets/14/problems/797 这题一开始考虑时犯了一些错误,一开始是这样考虑的,按题目的意思,每2分钟蠕虫爬行的距离是(U-D)寸.那么平均每分钟爬行的距离是(U-D+1)/2寸.然后对照样例数据N=12,U=3,D=1算出需要8分钟爬完,当然这个结果是错的.调整一下思路,假设时间为t,爬行距离为s.当t=0时,s=0:当t=1时,s=U:当t=2时,s=U-D:当t=3时,s=U-D+U:当t=4时,S=U-D+U-D:.

PAT:循环-07. 爬动的蠕虫(15) 错两个

#include<stdio.h> #include<math.h> int main() { int a,b,c,tmp=0,time=0,tag=0; scanf("%d%d%d",&a,&b,&c); while(tmp<=a) { if(tag==0) { ++time; tmp+=b; tag=1; } else { ++time; tmp-=c; tag=0; } } printf("%d\n",ti

今天天气很好,写个快排练练手

最近有些懒,好久没写代码了,现在写个快排练练手吧. public class QucikSort { //此处交换两个数 public static void swap(int a[],int low,int high) { int temp=a[low]; a[low]=a[high]; a[high]=temp; } //分区,分成两部分 public static int partion(int a[],int low,int high) { swap(a,low,high); int c

Autodesk View and Data API练练手

大家如果参加过我们的活动,你应该已经听过看过不少关于View and Data Web Service的例子里,如果还没有的话,请看看下面这几篇: http://www.cnblogs.com/junqilian/category/594048.html 如果你已经了解了Viewer,那有没有兴趣练练手,把这样酷的三维模型嵌入到你自己的网页中呢?那么开始练练手吧. ?体验代码资料下载:http://pan.baidu.com/s/15zZMQ 在下载解压缩后你应该可以看到下面的目录结构,其中ha

SQL点滴25—T-SQL面试语句,练练手

原文:SQL点滴25-T-SQL面试语句,练练手 1. 用一条SQL语句查询出每门课都大于80分的学生姓名 name   kecheng    fenshu 张三     语文     81张三     数学     75李四     语文     76李四     数学     90王五     语文     81王五     数学     100王五     英语     90 思路:这里不能直接用分数>80这样的比较条件来查询的到结果,因为要求没门成绩都大于80.我们可以反过来思考,如果有

YT14-先来练练手之蜜蜂与蜂房

Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input 输入数据的第一行是一个整数N,表示测试实例的个数,然后是N 行数据,每行包含两个整数a和b(0<a<b<50). Output 对于每个测试实例,请输出蜜蜂从蜂房a爬到蜂房b的可能路线数,每个实例的输出占一行. Sample Input 2 1 2 3 6 Sample Output 1 3 代码如下: