0
Help please!!!
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.
12 ответов
+ 5
var depth=31; //make this depth to be get from user
var climb=7;
var back=2;
var days=0;
var length=0;
while(length<depth){
length=length+climb; // climbed 7 feet up
days=days+1; // one day over
if(length>=depth)
break;
length=length-back; // climbing 2 feet back
}
document.write(days);
+ 5
What is your question?
+ 4
Hi! Please, show us your attempt!
+ 3
Please post your attempt
+ 3
Not necessarily you can do it without loops too
+ 2
Your code is coping the depth covered but it should print the days
+ 2
count the number of cycles? to do this, you can use a separate variable and increase it by one in the loop. or use the i variable, which you did
+ 1
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
var x= 7-2;
for(var i =x;i<=depth;i +=x){
console.log(i);
if(i===25){
i=i+2;
}
}
}
How to calculate the number of loops???
+ 1
How??? Atul
+ 1
Can you rewrite code for me? Ярослав Вернигора(Yaroslav Vernigora)
+ 1
No, you must do it yourself. Follow me:
your program is very wrong. think like a snail: you should create a loop and each iteration at the beginning of the loop add 7 feet, then check to see if the snail has come out of the well, if so, exit the loop. if the snail has not yet emerged, it slides two feet and the cycle resumes
+ 1
Thank you very much
Arun Ruban SJ