A:爬动的蠕虫
(输入文件:A.in输出文件,estdout.pc2)
一条蠕虫长度为1厘米在一口深度为n厘米的井底。已知蠕虫没分钟可以向上爬u厘
米。但必须休息1分钟后才能接着往上爬,在休息的过程中,蠕虫又下滑了d厘米,这样反
复进行上爬和下滑过程,请求出蠕虫需要多少时间才能爬出井。
假定
1)初始时始时螨虫趴在并底(高度为0):
2)上爬过程中,蠕虫头部到达井的项部就算出来:
3)计算时间时。不足一分钟按一分钟计算。
输入数据
测试数据有多组,每组一行3个数据,第一个表示井深n,第二个表示蠕虫每分钟上爬距
离u.第三个表示螺虫每次休息下滑距离d.
输出数据:
对应每组输入数据有一 行输出数据,当楼虫能够从深井造出时,输出其所重要的时间(分
钟数)当蠕虫不能从深井逃出时, 输出提示信息,“The worm can’t escapes from well.”
样例输出:
52 50 5
20 67 17
71 37 37
83 40 24 //7
78 67 8
56 12 25
4 37 7
样例输出:
3
1
The worm can’t escapes from well.
7
3
The worm can’t escapes from well.
1
Java代码:
public class Main {
@SuppressWarnings("resource")
public static void main(String[] args) {
int n, u, d;
int high;
int time;
Scanner input = new Scanner(System.in);
while(true) {
n = input.nextInt();
u = input.nextInt();
d = input.nextInt();
//System.out.println(n+" "+u+" "+d);
if(u <= d) {
System.out.println("The worm can't escapes from well.");
}else {
// System.out.println(getTime(n,u,d));
high = 0;
time = 0;
while(true) {
high += u;
time++;
if(high >= n) {
System.out.println(time);
break;
}else {
high -= d;
time++;
}
}
}
}
}
}