0
Depth
Help plz The snail climbs up 7 feet each day and slips back 2 feet each night. How many days will it take the snail to get out of a well with the given depth? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will reach 32 feet and get out of the well at day, without slipping back that night.
5 Antworten
+ 3
J.cartoonist
Sorry to say buddy, but your code is all over the place.
You have set up 2 vars count & days. Maybe rename count to climb for clarity.
Also, we don't know how many times we need to iterate because we don't know how deep the well is, so let's use a while loop.
The logic of the code is:
while the climb is <= depth
the snail must climb +=7 feet
this takes += 1 day
if the snail has climbed high enough, break the loop
else the snail must rest and slips down the climb -= 2 feet
next day this sequence repeats.
When the loop is broken
print the days
+ 2
Simple declear two variables amd store 7-2 result in any of one Variable like this
var a
var b=7-2;
Herr in first day you need to multiple by i set i =0 then value will be like this value =i *5 +b
Then second day u need to increment value of i use while loop here now this time i will be 1 so
1*5+ b
Same for day 3
2*5+b
....
....
....
6 th day
5*5+b
Your problem solved
+ 1
J.cartoonist
Can you attach your code for us to look at.
It makes it easier to assist you if we can see your concept
0
I already got the loop that I'm supposed to use to get the values within the range of the given depth....but I can't seem to be able to get the day before the depth or even the depth position between the days
0
Here's my code....
fun main(args: Array<String>) {
depth(31)
}
fun depth(depth:Int){
var count=0
var days=0
for(i in count..depth){
count+=7-2
days+=1
if(count<=depth){
println("$days day: $count")
}else{
break
}
}
println ("::::::")
println("$days, days to get out")
}