爬动的蠕虫

来源:互联网 发布:软件实施工程师简历 编辑:程序博客网 时间:2024/04/29 10:07

Description

一条蠕虫长度为1厘米,在一口深度为n厘米的井底。已知蠕虫每分钟可以向上爬u厘米,但必须休息1分钟后才能接着往上爬。在休息的过程中,蠕虫又下滑了d厘米。这样反复进行上爬和下滑过程,请求出蠕虫需要多少时间才能爬出井。

假定:

1)初始时蠕虫趴在井底(高度为0);

2)上爬过程中,蠕虫头部到达井的顶部就算出井;

3)计算时间时,不足一分钟按一分钟计算。

Input

测试数据有多组,每组一行3个数据,第一个表示井深n(0<n<1000),第二个表示蠕虫每分钟上爬距离u(0<u<1000),第三个表示蠕虫每次休息下滑距离d(0<d<1000)。

Output

对应每组输入数据有一行输出数据,当蠕虫能够从深井逃出时,输出其所需要的时间(分种数);当蠕虫不能从深井逃出时,输出提示信息:"The worm can't escape from the well."。

Sample Input

52 50 5
20 67 17
71 37 37
83 40 24
78 67 8
56 12 25
4 37 7

Sample Output

3
1
The worm can't escape from the well.
7
3
The worm can't escape from the well.
#include <stdio.h>#include <iostream>using namespace std;int main(){    int n,u,d,i=0,a[100001]={0},j;    while(scanf("%d %d %d",&n,&u,&d)!=EOF)    {        if(d<u||(u>=n))        {            while(n>0)            {                n=n-u;                a[i]++;                if(n<=0)                    break;                else                {                    n=n+d;                    a[i]++;                }                             }        }        i++;    }    j=0;    while(j<i)    {        if(a[j]!=0)            printf("%d\n",a[j]);        else            printf("The worm can't escape from the well.\n");        j++;    }    return 0;}

1

0 0
原创粉丝点击