Record a boring analysis: an array of random values, get the maximum and minimum multiplication formula

Loop to obtain the maximum and minimum composition of different lengths

let arr = ["1", "2"];
// 获取1~9的组合结果
for (let i = 3; i < 10; i++) {
    
    
   arr.push(i + "");
   for (let j = 1; j < arr.length; j++) {
    
    
   	  this.result = [];
      this.fun1(arr, "", arr.length, j);
   }
}
// arr--待组合数值的数组, str--拼接后的数值,len--arr长度,cutNum--第一个乘数长度
fun1(arr, str, len, cutNum) {
    
    
   let num = 0;
   // 循环拼接所有组合顺序
   for (let i = arr.length - 1; i >= 0; i--) {
    
    
      let item = str + arr[i];
      let otherArr = JSON.parse(JSON.stringify(arr));
      otherArr.splice(i, 1);
      num++;
	  
      if (otherArr.length) {
    
    
      	 // 回调:革命尚未成功,继续努力
         this.fun1(otherArr, item);
      } else {
    
    
      	 // 本次循环结束
         this.result.push(item);
      }
   }
   
   // 执行下一步,切分 && 计算
   if (num === len) this.fun2(this.result, cutNum);
},

fun2(data, cutNum) {
    
    
  let result = {
    
    
     maxStr: "",
     minStr: "",
     max: 0,
     min: 0,
  };
  
  for (let item of data) {
    
    
  	 // 切分乘数1 && 乘数2
     let str1 = +item.substring(0, cutNum);
     let str2 = +item.substring(cutNum, item.length);
     let str3 = str1 * str2;

	 // 暂存最大值 && 公式
     if (str3 > result.max) {
    
    
        this.$set(result, "max", str3);
        this.$set(result, "maxStr", `${
      
      str1} * ${
      
      str2} = ${
      
      str3}`);
     }

	 // 暂存最小值 && 公式
     if (result.min === 0 || str3 < result.min) {
    
    
        this.$set(result, "min", str3);
        this.$set(result, "minStr", `${
      
      str1} * ${
      
      str2} = ${
      
      str3}`);
     }
  }
  
  // 打印结果
  console.log("fun2:", result);
},

The results obtained

{
    
    maxStr: "3 * 21 = 63", minStr: "1 * 23 = 23", max: 63, min: 23}
{
    
    maxStr: "21 * 3 = 63", minStr: "23 * 1 = 23", max: 63, min: 23}
{
    
    maxStr: "4 * 321 = 1284", minStr: "1 * 234 = 234", max: 1284, min: 234}
{
    
    maxStr: "41 * 32 = 1312", minStr: "24 * 13 = 312", max: 1312, min: 312}
{
    
    maxStr: "321 * 4 = 1284", minStr: "234 * 1 = 234", max: 1284, min: 234}
{
    
    maxStr: "5 * 4321 = 21605", minStr: "1 * 2345 = 2345", max: 21605, min: 2345}
{
    
    maxStr: "52 * 431 = 22412", minStr: "13 * 245 = 3185", max: 22412, min: 3185}
{
    
    maxStr: "431 * 52 = 22412", minStr: "245 * 13 = 3185", max: 22412, min: 3185}
{
    
    maxStr: "4321 * 5 = 21605", minStr: "2345 * 1 = 2345", max: 21605, min: 2345}
{
    
    maxStr: "6 * 54321 = 325926", minStr: "1 * 23456 = 23456", max: 325926, min: 23456}
{
    
    maxStr: "63 * 5421 = 341523", minStr: "13 * 2456 = 31928", max: 341523, min: 31928}
{
    
    maxStr: "631 * 542 = 342002", minStr: "246 * 135 = 33210", max: 342002, min: 33210}
{
    
    maxStr: "5421 * 63 = 341523", minStr: "2456 * 13 = 31928", max: 341523, min: 31928}
{
    
    maxStr: "54321 * 6 = 325926", minStr: "23456 * 1 = 23456", max: 325926, min: 23456}
{
    
    maxStr: "7 * 654321 = 4580247", minStr: "1 * 234567 = 234567", max: 4580247, min: 234567}
{
    
    maxStr: "74 * 65321 = 4833754", minStr: "13 * 24567 = 319371", max: 4833754, min: 319371}
{
    
    maxStr: "742 * 6531 = 4846002", minStr: "135 * 2467 = 333045", max: 4846002, min: 333045}
{
    
    maxStr: "6531 * 742 = 4846002", minStr: "2467 * 135 = 333045", max: 4846002, min: 333045}
{
    
    maxStr: "65321 * 74 = 4833754", minStr: "24567 * 13 = 319371", max: 4833754, min: 319371}
{
    
    maxStr: "654321 * 7 = 4580247", minStr: "234567 * 1 = 234567", max: 4580247, min: 234567}
{
    
    maxStr: "8 * 7654321 = 61234568", minStr: "1 * 2345678 = 2345678", max: 61234568, min: 2345678}
{
    
    maxStr: "85 * 764321 = 64967285", minStr: "13 * 245678 = 3193814", max: 64967285, min: 3193814}
{
    
    maxStr: "853 * 76421 = 65187113", minStr: "135 * 24678 = 3331530", max: 65187113, min: 3331530}
{
    
    maxStr: "8531 * 7642 = 65193902", minStr: "2468 * 1357 = 3349076", max: 65193902, min: 3349076}
{
    
    maxStr: "76421 * 853 = 65187113", minStr: "24678 * 135 = 3331530", max: 65187113, min: 3331530}
{
    
    maxStr: "764321 * 85 = 64967285", minStr: "245678 * 13 = 3193814", max: 64967285, min: 3193814}
{
    
    maxStr: "7654321 * 8 = 61234568", minStr: "2345678 * 1 = 2345678", max: 61234568, min: 2345678}
{
    
    maxStr: "9 * 87654321 = 788888889", minStr: "1 * 23456789 = 23456789", max: 788888889, min: 23456789}
{
    
    maxStr: "96 * 8754321 = 840414816", minStr: "13 * 2456789 = 31938257", max: 840414816, min: 31938257}
{
    
    maxStr: "964 * 875321 = 843809444", minStr: "135 * 246789 = 33316515", max: 843809444, min: 33316515}
{
    
    maxStr: "9642 * 87531 = 843973902", minStr: "1357 * 24689 = 33502973", max: 843973902, min: 33502973}
{
    
    maxStr: "87531 * 9642 = 843973902", minStr: "24689 * 1357 = 33502973", max: 843973902, min: 33502973}
{
    
    maxStr: "875321 * 964 = 843809444", minStr: "246789 * 135 = 33316515", max: 843809444, min: 33316515}
{
    
    maxStr: "8754321 * 96 = 840414816", minStr: "2456789 * 13 = 31938257", max: 840414816, min: 31938257}
{
    
    maxStr: "87654321 * 9 = 788888889", minStr: "23456789 * 1 = 23456789", max: 788888889, min: 23456789}

to sum up

1代表放入乘数12代表放入乘数2。

最大值:倒序排序 => 122121212...
最小值:顺序排序 => 12121212...2(无论长度,最后一位放入乘数2

Guess you like

Origin blog.csdn.net/qweqwe2277/article/details/111037593