573 - The Snail

Problem Link

import java.util.*;
public class uva573 {
public static void main(String [] args) {
Scanner scn = new Scanner(System.in);
double h = 1;
while( h != 0) { //input for the height of well
h = scn.nextInt();
double u = scn.nextDouble();//input for distance that snail can climb on the day
double d = scn.nextDouble();//input for distance that snail will drop down on the night
double f = scn.nextDouble();//input the percent fatigue after the successive day
double percentfatigue = u*f*0.01;
int day = 0;
int t = 0;
double distanceclimbed = u;
double total = 0;
while(total < h && total >= 0) {
total = total + distanceclimbed;
if(total > h) {
t = 1;
day++;
break;
}
else if(total < 0) {
t = 0;
}
total = total - d;
distanceclimbed = distanceclimbed - percentfatigue;
if(distanceclimbed < 0)
distanceclimbed = 0.0;
day++;

}
if(t == 1 && h > 0)
System.out.println("success on day "+day);
else if(t == 0 && h > 0)
System.out.println("failure on day "+day);
}
}
}

Share this

Related Posts

Previous
Next Post »