蜗牛每天爬7英尺,晚上滑回2英尺,蜗牛要花多少天才能在一定深度下走出井?示例输入31样例输出6这是我写的,但它没有工作。
我的代码是
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
var day=0;
var dist=0;
while(dist<depth-2){
day++;
dist=dist+5;
}
console.log(day)
}
还有别的办法吗?
发布于 2022-05-15 13:06:12
蜗牛每天爬升5英尺,最后一天爬升到7英尺。这使得
function main() {
var depth = parseInt(readLine(), 10);
console.log(Math.max(1, Math.ceil((depth - 2) / 5)));
}
示例:
for (let depth = 1; depth < 32; ++depth) {
console.log('depth: ' + depth + ', days: ' + Math.max(1, Math.ceil((depth - 2) / 5)));
}
https://stackoverflow.com/questions/72248394
复制相似问题