There is a frog in a well having 30-meter depth. One day frog plan to escape the well in a day frog can jump only 3 meters of height and tired up and took rest the whole night, in the night he sleeps down by two-meter of height. continuously frog did his duty daily.
So the question is how many days the frog took to get out of the well.
Input: depth=30
Output: 28 days
Approach
Java
public class FrogWell {static int depthOfWell=30;//in meterstatic int disClimbInDay=3;//in meterstatic int slipInNight=2;//in meterstatic int noOfDays= 0;public static void main(String[] args) {System.out.println(calculate(0));}private static int calculate(int distCovered) {noOfDays++;//during daydistCovered=distCovered+disClimbInDay;while(distCovered<depthOfWell) {//during nightdistCovered= distCovered-slipInNight;calculate(distCovered);return noOfDays;}return noOfDays;}}
No comments:
Post a Comment