【问题标题】:Split an array of numbers to arrays whose sum is less or equal than a given number将数字数组拆分为总和小于或等于给定数字的数组
【发布时间】:2013-08-04 15:04:16
【问题描述】:

如果数组是:[1,2,3,4,5,6,7,8,9,10]
最大和为 25
该脚本应创建三个数组
arr1:[1, 2, 3, 4, 5, 6] (sum=21)
arr2:[7,8,9] (sum=24)
arr3:[9,10] (sum=19)
我可以创建第一个数组,但不能创建其他数组,有人可以帮助我吗?

我的 jquery 代码是:

$(document).ready(function(){
  numbers=[1,2,3,4,5,6,7,8,9,10]
  total = 0;
  newOne =[];
  for(i = 0; i < numbers.length; i++) {
       if(total<= (25-numbers[i])){
        total += numbers[i];
        newOne.push(numbers[i]);
       };
    };
  numbers.splice(0,newOne.length);
  console.log(newOne);  
  console.log(numbers);     
});

谢谢大家

【问题讨论】:

  • 分割数组可以只有一个元素吗?
  • 给定的数是小数怎么样,比如5,如何拆分数组的其余部分?
  • 这个问题有什么限制?原始数组是否总是从一到某个限制的数字?是否总是需要获得三个子集? (显然子集不必是不相交的;对吗?)当问到数论问题时,如果问题也用数学术语来表述会节省很多时间。
  • 您可以将numbers 数组中的第一项移动到末尾,然后再次循环遍历它,重复此过程,直到您完成了数组中的每个项。

标签: javascript jquery arrays algorithm


【解决方案1】:

也许简单一点:

$(document).ready(function(){
  var numbers=[1,2,3,4,5,6,7,8,9,10]
  var total;
  var newOne = [];
  var index = -1;
  while (numbers.length) {
    total = 0;
    index++;
    newOne[index] = []
    while (total + numbers[0] <= 25 ) {
      total += numbers[0];
      newOne[index].push(numbers.shift());
    }
  }  
  console.log(newOne);  
  console.log(numbers);     
});

【讨论】:

    【解决方案2】:

    类似

    $(document).ready(function(){
        numbers = [1,2,3,4,5,6,7,8,9,10];
        total = 0;
        coll = [];
        newOne = null;
        for (i = 0; i < numbers.length; i++) {
            if (newOne !== null && total + numbers[i] <= 25) {
                total += numbers[i];
                newOne.push(numbers[i]);
            } else {
                // We enter in the else for i = 0 and when we have to
                // create a new subarray
                if (newOne !== null)
                {
                    console.log(newOne);
                }
    
                total = numbers[i];
                newOne = [ numbers[i] ];
                coll.push(newOne);
            }
        }
    
        // We have to print the last subarray (because normally
        // a subarray is printed only when it's filled)
        console.log(newOne);     
    }
    

    我不拼接原始数组。我什至把coll所有的各种子数组都放进去了。

    如果你只想在最后打印数组:

    numbers = [1,2,3,4,5,6,7,8,9,10];
    total = 0;
    coll = [];
    newOne = null;
    for (i = 0; i < numbers.length; i++) {
        if (newOne !== null && total + numbers[i] <= 25) {
            total += numbers[i];
            newOne.push(numbers[i]);
        } else {
            total = numbers[i];
            newOne = [ numbers[i] ];
            coll.push(newOne);
        }
    }
    
    // Here we print the subarrays
    for (i = 0; i < coll.length; i++)
    {
        console.log(coll[i]);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2014-05-26
      相关资源
      最近更新 更多