2

虽然都是很简单的算法,每个都只需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
453 声望27 粉丝

代码胜于雄辩