【问题标题】:How to accumulate over each number? JavaScript [closed]如何累积每个数字? JavaScript [关闭]
【发布时间】:2020-11-01 18:33:04
【问题描述】:

这是我的问题,我很难解决这个问题 任务:我们将向您传递一个由两个数字组成的数组。 返回这两个数字的总和加上 它们之间所有数字的总和。最低的数字并不总是排在第一位。

For example, sumAll([4,1]) should return 10 because
sum of all the numbers between 1 and 4 (both inclusive) is 10.

function sumAll(arr) {
  Math.min(arr); //finds the lowest number and takes it 1
  Math.max(arr); //finds the largest number 4
  //must start at the 1st number and loops over until the max value is reached
  //0 start at the 0th index of the array 
  //++ increament by one so 1 2 3 4 
  //multiply's each number
  //.lenght until the lenght of the array is reached
  var i;
  for (i = 0; i < arr.length; i++) {
    i * i;
  }
  return 1;
}

sumAll([1, 4]);

【问题讨论】:

  • 数组中是否总是有 2 个数字?
  • 在 O(1) 可以做到的情况下,每个人都在使用循环...function sum(arr) { const start = arr[0], end = arr[1]; return (end - start + 1) * (start + end) / 2; }
  • @D.Pardal 添加一个答案,但一定要先排序
  • @segFault 我正要这样做,但问题被关闭了......

标签: javascript loops max min


【解决方案1】:

您可以在运行循环之前找到较小的数字,以获得所有中间数字的总和。

你可以只添加条件:

    if(arr[0]<arr[1]){
     first= arr[0], last= arr[1]
     }
   else { 
    first=arr[1], last=arr[0] }
  for (i = first; i <= last; i++){
     let temp = temp + i;
    }
   return temp;
  }

【讨论】:

    【解决方案2】:

    只需对数组进行排序并运行循环以添加数字,从第一个元素开始到第二个元素结束

    function findSum(arr){
        let sortedArr = arr.slice().sort((a,b) => a-b);
        let total =0;
        for(let i=arr[0];i<=arr[1];i++){
            total+=i;
    }
    console.log(total);
    }
    
    findSum([1,4])

    【讨论】:

    • 投反对票的人也可以说明原因吗?非常需要理解这个问题。不鼓励不必要的反对票!
    【解决方案3】:

    如果数组中总是有 2 个数字,那么您可以轻松做到这一点,而无需编写任何花哨的代码。

    var arr = [1, 4];
    arr.sort((a, b) => a - b);
    var total = 0;
    for (var i = arr[0]; i <= arr[1]; i++ ) {
         total += i;
    }
    
    console.log(total);
    

    【讨论】:

    • 第一个索引不一定是最低的,这是主要问题。
    • 我知道了,我没注意到,那他就先排序好了。答案已更新。
    【解决方案4】:
    var points = [40, 100, 1, 5, 25, 10]; 
    points.sort(function(a, b){return a-b});
    points[0]; // this is min value of the array values
    

    You can check this link on w3schools

    【讨论】:

      【解决方案5】:

      您可以通过多种方式做到这一点,在这种情况下,我使用了一个 while 循环。

      function sumAll(arr) {
        // Get the min/max values from the array,
        // Note: you have to spread the array values as individual args using '...' notation
        const min = Math.min(...arr);
        const max = Math.max(...arr); 
        
        // Start at the min value
        let current = min;
        let sum = 0;
        // Loop through all numbers between min and max inclusively
        while (current <= max) {
          sum += current;
          current++;
        }
        
        return sum;
      };
      
      console.log(sumAll([1, 4]));

      【讨论】:

        【解决方案6】:

        您可以使用Math.max 从输入数组中获取最大数,使用Math.min 从数组中获取最小数,您只需要将数组中的spread 值放入方法调用中,以便从输入数组用作参数(而不是数组本身)。

        一旦你有了最大和最小的数字,你就可以找到这两个数字之间(包括)的总和。这可以使用循环来完成。但是,更有效的方法是使用公式为您计算它。如果你叫小号a和大号b,你想找到:

        res     = a + (a+1) + (a+2) + ... + (b-1) + b
        res2    = b + (b-1) + (b-2) + ... + (a+1) + a
        

        正如你在上面看到的res2res 是相等的。所以我们可以说res2 = res。所以,如果我们执行res + res2,我们将得到2*res。如果我们将两者相加(按列相加),我们得到:

        2*res = a+b + (a+1)+(b-1) + (a+2)+(b-2) + ... + (b-1)+(a+1) + b+a
              = a+b +     a+b     +     a+b     + ... +     a+b     + a+b
        

        如您所见,2*res 导致 a+b 被重复用于原始方程中的每个数字,即 b-a + 1 次。因此:

        2*res = (b-a + 1)*(a+b)
        

        由于我们要查找res是什么,我们可以将两边除以2得到:

        res = (b-a + 1)*(a+b)/2
        

        所以,我们可以使用上面的等式求两个数字ab之间的数字之和,其中a是较小的数字,b是较大的数字。

        同时使用Math.max()Math.min() 和上面的等式,我们可以使用以下方法做到这一点:

        const sumRange = (a, b) => ((b - a + 1)*(a + b))/2;
        function sumAll(arr) {
          const smaller = Math.min(...arr);
          const bigger = Math.max(...arr);
          return sumRange(smaller, bigger); 
        }
        
        console.log(sumAll([4, 1]));

        【讨论】:

          猜你喜欢
          • 2018-09-20
          • 1970-01-01
          • 2012-04-19
          • 1970-01-01
          • 2013-08-17
          • 2013-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多