【问题标题】:CodeSignal Challenge: Phone CallCodeSignal 挑战:电话
【发布时间】:2018-09-11 15:47:31
【问题描述】:

challenge 是:

部分电话使用率可描述如下:

  • 通话的第一分钟只需 1 美分
  • 从 2 日到 10 日(含)每分钟的费用为 min2_10 美分
  • 第 10 次后的每分钟花费最少 11 美分。

在通话之前,您的帐户中有 s 美分。您可以拥有的最长通话时长(以分钟为单位,向下舍入到最接近的整数)?

我的代码:(我的代码没有通过 3 个隐藏测试)

function phoneCall(min1, min2_10, min11, s) {
    
    //Declaring Variables
    let minutes = 0;
    let taken = 0;
    
    //Checks if there is atleast one minute to charge for
    if (s >= min1) {
        minutes = minutes + 1;
        s = s - min1;
        //Console Update
        console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
    }
    
    //Checks how many minutes it can call for in between 2 and 10
    for (let i = 0; i < 9; i++) {
        if (s > 1) {
            minutes = minutes + 1;
            s = s - min2_10;
            //Console Update
            console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
        } 
        
        
        //Checks if minutes equals 10 and then checks for how many minutes it can afford
        if (minutes === 10) {
            if (s >= 1) {
                minutes = minutes + Math.floor(s / min11);
                taken = taken + Math.floor(s / min11); 
                s = s - (taken * min11);
                //Console Update
                console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
            }
        }
    }
    
    //Returns the minutes
    return minutes;
}

【问题讨论】:

  • 这对于 StackOverflow 来说太宽泛了。请调试您的代码并返回具体问题。
  • 您的问题是什么?它告诉你什么不工作?该测试在哪里进行?我不知道如何帮助你,因为我不知道出了什么问题。
  • 您的方法最少需要 3 个单独的变量,这似乎很奇怪。为什么它不只占用通话时间和 s?
  • 我不能告诉你哪里出了问题,因为它们是隐藏的测试,我不知道什么是行不通的,所以如果你认为我的范围很广,那不是我的错。跨度>
  • 欢迎来到 Stack Overflow!请访问help center,使用tour 了解内容和How to Ask。此外,您的 sn-p 实际上并没有做任何事情

标签: javascript ecmascript-6


【解决方案1】:

刚刚发现什么不工作!

if 语句写为“if (s > 1) {}”,但写得不正确,因为它不适用于所有类型的输入,因此我将其更改为“if (s >= min2_10) { }”。

这是代码:

function phoneCall(min1, min2_10, min11, s) {
    
    //Declaring Variables
    let minutes = 0;
    let taken = 0;
    
    //Checks if there is atleast one minute to charge for
    if (s >= min1) {
        minutes = minutes + 1;
        s = s - min1;
        //Console Update
        console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
    }
    
    //Checks how many minutes it can call for in between 2 and 10
    for (let i = 1; i < 10; i++) {
        if (s >= min2_10) {
            minutes = minutes + 1;
            s = s - min2_10;
            //Console Update
            console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
        } 
        
        
        //Checks if minutes equals 10 and then checks for how many minutes it can afford
        if (minutes === 10) {
            if (s >= 1) {
                minutes = minutes + Math.floor(s / min11);
                taken = taken + Math.floor(s / min11); 
                s = s - (taken * min11);
                //Console Update
                console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
            }
        }
    }
    
    //Returns the minutes
    return minutes;
}

【讨论】:

    【解决方案2】:
    function phoneCall(min1, min2_10, min11, s) {
    
      let result = s - min1 - (min2_10 * 9);
      let minLeft = Math.floor(result / min11);
      //case if have upto min1 only
      if (min1 === s) return 1;
      //case if have cents upto 10min or less and above 1min
      if (result < 0) return 1 + Math.floor((s - min1) / min2_10);
      //case if have cents more than 10min 
      //1min+9min for min2 to 10 mins = 10min
      return 10 + minLeft;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 2018-01-21
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 2020-01-16
      • 2017-06-23
      • 2013-12-21
      • 1970-01-01
      相关资源
      最近更新 更多