题意: 一只蜗牛在井底,他要爬出井。给出以下条件。
1.白天能爬一段距离,晚上必须得掉一段距离。
2.每次白天爬的距离会因为疲劳度减少,减少的距离根据第一天爬的距离和经过几天决定。
3.疲劳度在怎么样,蜗牛白天不会往下掉,如果不能爬,他就得呆在原地,等晚上往下掉,一只掉到底部。
4.必须当与井底距离小于0时才算fail,当刚刚好处于井底0位置处,继续往上爬,不算fail。
代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner cin=new Scanner(System.in);
while(cin.hasNext()){
double wellHeight=cin.nextDouble();
double dayFeet=cin.nextDouble();
double nightFeet=cin.nextDouble();
double fatigue=cin.nextDouble();
if(wellHeight==0)return;
double height=0;
int days;
double temp=0;
for(days=0;height>=0;days++){
temp=dayFeet-dayFeet*(fatigue*0.01)*days;
if(temp<0)temp=0;
height=height+temp;
if(wellHeight<height){
break;
}
height=height-nightFeet;
}
System.out.println(height<0 ? "failure on day "+days : "success on day "+(days+1));
}
}
}