美文网首页
2020-05-27 5 kyu How Many Number

2020-05-27 5 kyu How Many Number

作者: 苦庭 | 来源:发表于2020-05-27 16:24 被阅读0次

    https://www.codewars.com/kata/55f5efd21ad2b48895000040

    My answer / AC

    function maxSumDig(nmax, maxsm) {
      var res = [];
      var isSatisfy;
      for(var i=1000; i<=nmax; i++) {
        let arr = i.toString().split("");
        isSatisfy = true;
        for(var j=0; j<=arr.length-4; j++){
         if(Number(arr[j]) + Number(arr[j+1]) + Number(arr[j+2]) + Number(arr[j+3]) > maxsm) isSatisfy = false;
        }
        if(isSatisfy) res.push(i);
      }
      var sum = res.reduce((a,b)=>Number(a)+Number(b));
      var mean = sum/res.length;
      var meanmap = res.map(a=>Math.abs(Number(a)-Number(mean)));
      var index = meanmap.indexOf(Math.min(...meanmap));
      return [res.length, res[index], sum];
    }
    

    Recap

    • 不算难,我的答案和最佳也基本一致
    • 主要问题是搞错了题目的用意,没看清”the maximum sum of 4 contiguous digits is maxSum“,是一个大数字中每连续四个数进行一次是否小于maxSum判断

    相关文章

      网友评论

          本文标题:2020-05-27 5 kyu How Many Number

          本文链接:https://www.haomeiwen.com/subject/okawahtx.html