逐日一道算法题 - KaprekarsConstant(hard-1)

虽然都是很简单的算法,每一个都只需5分钟摆布,但写起来总会碰到差别的小问题,愿望大家能跟我一同天天提高一点点。
更多的小算法演习,能够检察我的文章。

划定规矩

Using the JavaScript language, have the function KaprekarsConstant(num) take the num parameter being passed which will be a 4-digit number with at least two distinct digits. Your program should perform the following routine on the number: Arrange the digits in descending order and in ascending order (adding zeroes to fit it to a 4-digit number), and subtract the smaller number from the bigger number. Then repeat the previous step. Performing this routine will always cause you to reach a fixed number: 6174. Then performing the routine on 6174 will always give you 6174 (7641 - 1467 = 6174). Your program should return the number of times this routine must be performed until 6174 is reached. For example: if num is 3524 your program should return 3 because of the following steps: (1) 5432 - 2345 = 3087, (2) 8730 - 0378 = 8352, (3) 8532 - 2358 = 6174.

运用JavaScript言语,让函数KaprekarsConstant(num)猎取 通报的num参数,该参数将是一个4位数字,至少有两个差别的数字。
按降序和升序分列数字(增加零以使其合适4位数字),用较大的数字去较小的数字。
然后反复上一步,直到相减效果即是牢固数字:6174,返回该顺序必需实行的次数。
比方:假如传入的参数为3524,你的顺序应当返回3,即该顺序必需实行3次才获得6174的效果。
由于以下步骤:(1)5432 – 2345 = 3087,(2)8730 – 0378 = 8352,(3)8532 – 2358 = 6174.

function KaprekarsConstant(num) { 

  // code goes here  
  return num; 
}
   
// keep this function call here 
KaprekarsConstant(3524);    

测试用例

Input:2111
Output:5

Input:9831
Output:7

my code

function KaprekarsConstant(num) {
  var count = 0
  while (true) {
    var maxNum = num
      .toString()
      .split('')
      .sort((item1, item2) => item2 - item1)
      .join('')
    maxNum = Number(maxNum)

    var minNum = num
      .toString()
      .split('')
      .sort()
      .join('')
    minNum = Number(minNum)

    num = '0000' + (maxNum - minNum)
    num = num.substr(-4)
    count++

    if (num == 6174 || num == 0) break
  }

  return count
}

other code

code-1

function KaprekarsConstant(num) {
    const KAP = 6174;
    var count = 0;
    while (true) {
        var num = evaluator(num)
        if (num === true) {
            return count;
        }
    }

    function evaluator(num) {
        count++
        console.log('count', count);
        var minNumArr = num.toString().split('').sort();
        var maxNumArr = minNumArr.slice(0).reverse();
        var littleNum = parseInt(minNumArr.join(''), 10);
        var bigNum = parseInt(maxNumArr.join(''), 10);
        while (bigNum < 1000) {
            bigNum = bigNum * 10;
        }

        return bigNum - littleNum === KAP ? true : bigNum - littleNum;
    }
}

code-2

function KaprekarsConstant(num) {
  var count = 0;
  while (num != 6174) {
    count += 1;
    var numArr = num.toString().split('');
    while (d.length < 4) {
      numArr.push('0');
    }
    var smaller = numArr.sort().join('');
    var bigger = numArr.reverse().join('');
    num = bigger - smaller;
  }
  return count;
}

code-3

function KaprekarsConstant(num) { 

    let count = 0;
    while (num != 6174) {
        let numArray = num.toString().split('').sort();
        
        let ascending = parseInt(numArray.join(''));
        let descending = parseInt(numArray.reverse().join(''));
        
        while (descending.toString().length < 4) {
            descending *= 10;
        }
        
        num = Math.abs(ascending - descending);

        count++;
        if (count > 999) break;  // failover
    }

    return count;
}

思绪

个人思绪:

  1. 本想运用递回去处理,代码量会更少更文雅,然后发明须要盘算实行的次数,所以才运用while去遍历实行(实在运用闭包也能够处理,无法问题挪用就是KaprekarsConstant(xxxx)
  2. 先把数字转成字符串再转数组,运用数组起落排序拿到最大最小值,相减以后获得效果
  3. 给相减效果补0,保证相减效果一向都是4位数
  4. 当相减效果即是6174或0时,中缀while轮回,返回实行次数

优化点:
1.minArr = num.toString().split(”).sort(); minNum = minArr.join(“”),那末maxNum能够直接运用minArr.reverse().join(”)获得
2.能够把if推断内容放到while上
3.补0能够运用 num * 10 的体式格局,而不是字符串补零

    原文作者:an_l
    原文地址: https://segmentfault.com/a/1190000015599867
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞