hdu 1302 The Snail

题意:求一只在井底的蜗牛是否可以爬出井口。

H表示井口的高度;

U表示蜗牛白天能够爬行的距离;

D表示晚上睡觉时,向下滑行的距离;

F表示懒惰百分数-每天少爬的距离U*F。

注意:

1)蜗牛当天爬行的距离比前一天的距离小;

2)sum使用double

3)u,f使用double值

4)蜗牛爬行的距离不小零

5)蜗牛爬行的高度总是会超过井口的高度或者变成负数。


java代码实现:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = 0, d = 0;
		double u = 0.0;
		double f = 0.0;
		while (sc.hasNext()) {
			String str = sc.nextLine();
			String[] strs = str.split(" ");
			h = Integer.parseInt(strs[0]);
			if (h == 0) {
				break;
			}
			u = Double.parseDouble(strs[1]);
			d = Integer.parseInt(strs[2]);
			f = u * Double.parseDouble(strs[3]) / 100;
			double sum = 0;
			int day = 0;
			while (true) {
				day++;
				sum += u;// 白天理想距离
				// System.out.println("sum1-" + sum);
				if (sum > h) {// 超过井口
					break;
				}
				if (u > 0) {// 懒惰导致距离小于0
					u -= f;// 懒惰导致白天爬行距离变小
					// System.out.println("u-" + u);
				} else {
					u = 0;
				}
				sum -= d;// 夜晚向下滑行距离
				// System.out.println(sum + " sum2-" + sum + "---" + d);
				if (sum < 0) {// 滑到底部
					break;
				}
			}
			if (sum >= h) {
				System.out.println("success on day " + day);
			} else {
				System.out.println("failure on day " + day);
			}
		}
	}

}




The Snail

蜗牛
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2004    Accepted Submission(s): 1413


Problem Description
A snail is at the bottom of a 6-foot well and wants to climb to the top. 
一口6英尺高的井下有一头蜗牛,他想爬到顶部。
The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. 
白天他最多可以爬3英尺高,当他晚上睡觉的时候,会向下滑行1英尺。
The snail has a fatigue factor of 10%, which means that on each successive day the snail climbs 10% * 3 = 0.3 feet less than it did the previous day. 
蜗牛有百分之10的概率会偷懒,说以一只蜗牛第天成功爬行距离的小于前一天的0.3英尺。
(The distance lost to fatigue is always 10% of the first day's climbing distance.) 
因为懒惰而丢失的距离是第一天的百分之10。
On what day does the snail leave the well, i.e., what is the first day during which the snail's height exceeds 6 feet? 
请问蜗牛哪天可以离开井口,意思就是蜗牛什么时候可以爬行6英尺的距离。
(A day consists of a period of sunlight followed by a period of darkness.) 
一天包含一个白天和黑夜。
As you can see from the following table, the snail leaves the well during the third day.
你可以看下列的表格,蜗牛在三天内离开井口。
Day Initial Height Distance Climbed Height After Climbing Height After Sliding 
爬到顶部后,直接向下滑行。
1 0 3 3 2
2 2 2.7 4.7 3.7
3 3.7 2.4 6.1 -

Your job is to solve this problem in general. 
你的任务是解决简单问题。
Depending on the parameters of the problem, the snail will eventually either leave the well or slide back to the bottom of the well. 
依靠问题给的参数,蜗牛要么离开井口要么滑回井底。
(In other words, the snail's height will exceed the height of the well or become negative.) 
换一句话说,蜗牛爬行的高度总是会超过井口的高度或者变成负数。
You must find out which happens first and on what day.
 你必须找到蜗牛第一次到达顶部或底部的时间。

Input
The input file contains one or more test cases, each on a line by itself. 
输入文件包含多个测试事件,每一行表示一个测试事件。
Each line contains four integers H, U, D, and F, separated by a single space. 
且每一行包含四个整数H, U, D, 和 F,使用空格隔开。
If H = 0 it signals the end of the input; otherwise, all four numbers will be between 1 and 100, inclusive. 
如果H=0,那么表示输入文件已经到了文件末尾,否则,数据都在1到100之间。
H is the height of the well in feet, 
H表示井口的高度;
U is the distance in feet that the snail can climb during the day, 
U表示蜗牛白天能够爬行的距离;
D is the distance in feet that the snail slides down during the night, 
D表示晚上睡觉时,向下滑行的距离;
and F is the fatigue factor expressed as a percentage. 
F表示懒惰百分数。
The snail never climbs a negative distance. 
蜗牛不会爬出一个负的距离。
If the fatigue factor drops the snail's climbing distance below zero, the snail does not climb at all that day. 
如果因为疲劳因素导致蜗牛爬行的距离小于0,那么蜗牛将不再爬行。
Regardless of how far the snail climbed, it always slides D feet at night.
 不管蜗牛爬行了都少距离,每天晚上向下滑行的距离D都不变。

Output
For each test case, output a line indicating whether the snail succeeded (left the well) or failed (slid back to the bottom) and on what day. 
对于每一个测试事件,在单独的一行输出两个结果,一个是蜗牛梦想结束的结果,第二个是梦想结束的时间。
Format the output exactly as shown in the example.
 下列案例将精确的解释输出格式。

Sample Input
  
  
6 3 1 10 10 2 1 50 50 5 3 14 50 6 4 1 50 6 3 1 1 1 1 1 0 0 0 0
 

Sample Output
  
  
success on day 3 failure on day 4 failure on day 7 failure on day 68 success on day 20 failure on day 2
 

Source
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1304  1303  1306  1305  1323 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值