簡體   English   中英

還有其他方法嗎?

[英]is there any other way of doing it?

蝸牛每天爬 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)
}

還有其他方法嗎

蝸牛每天爬 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))); }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM