简体   繁体   English

数组,整数和数组总和

[英]array, integers and sum of array

Problem: Write a method that takes two integers n and m as parameters, and return the sum of alle the numbers (array of numbers) from n (including n) to m (including m). 问题:编写一个方法,该方法将两个整数n和m作为参数,并将所有数字(数字数组)的总和从n(包括n)返回到m(包括m)。

public int getSum(int n, int m){
    this.n = n;
    this.m = m;
    int [] x = {n,m};
    int result = 0;
    for(int i=0; i<x.length; i++){
        result += x[i];
    }
    return result;
}

if n=1 and m=4 the method returns 5, but the method need to return 10, (1,2,3,4)=(1+2+3+4). 如果n = 1并且m = 4,则该方法返回5,但是该方法需要返回10,(1,2,3,4)=(1 + 2 + 3 + 4)。 Guess this line is not right int [] x = {n,m}; 猜猜这行不正确int [] x = {n,m}; since the array will be {1,4} if n=1 and m=4. 因为如果n = 1且m = 4,则数组将为{1,4}。 How to fix this, need to create the array {1,2,3,4} if n=1 and m=4 如何解决此问题,如果n = 1和m = 4,则需要创建数组{1,2,3,4}

try this: 尝试这个:

public int getSum(int n, int m){
    int result = 0;
    for(int i = n;i <= m;i++)
         result += i;
    return result;
}

or this: 或这个:

public int getSum(int n, int m){
    int result = 0;
    while(n <= m)
         result += n++;

    return result;
}

or maybe with funny math: 或有趣的数学:

public int getSum(int n, int m){
    return (m*(m+1)/2) - ((n-1)*((n-1)+1)/2)
}

F(n) = (n*(n+1)/2) = 0+1+2+...+n
F(m) - F(n-1) = n + n+1 + n+2 + ... + m

Java 8

return IntStream.rangeClosed(n, m).sum();

@Andres has already given a perfect solution. @Andres已经给出了完美的解决方案。 But I wanted to explain some more, so here goes. 但是我想解释更多,所以继续。

There are many problems with your code. 您的代码有很多问题。

But yes, the central problem is the line. 但是,是的,核心问题是线路。 int [] x = {n,m};

What that line does is creates an array of just 2 numbers, and when you iterate over that array you are only adding those two numbers, not all the number in between. 该行所做的是创建仅包含2个数字的数组,并且在该数组上进行迭代时,您仅添加了这两个数字,而不是两者之间的所有数字。

In languages like C, C++, Java, etc... the simplest idiom to iterate over a range of numbers is 在C,C ++,Java等语言中,迭代一系列数字的最简单的习惯用法是

for(int i = n, i <=m, i++){
  ...
}

What this does is, create a temporary variable i and initialize it to the first number in the range n . 这是创建一个临时变量i并将其初始化为n范围内的第一个数字。 And then run in the loop incrementing that temporary number i by one every time until it exceeds the last number of the range m . 然后循环运行,使该临时编号i每次递增1,直到超过范围m的最后一个编号。

Within the body of the loop each number of the range will now be available, for that iteration of the loop. 在循环体内,对于循环的该迭代,范围的每个数字现在都可用。

So in your case where you are summing up all the numbers, you can accumulate all those numbers in the range by setting up an accumulator variable like following. 因此,在对所有数字求和的情况下,可以通过设置如下所示的累加器变量来累加范围内的所有这些数字。

int acc = 0;
for(int i = n, i <=m, i++){
  acc = acc + i;
}

can't you just do this : 你不能只是这样做:

public static int getSum(final int n, final int m) {
    // default value
    int sum = 0;

    // not permitted
    if (n >= m)
        return 0;

    // all of the work here
    for (int i = n; i <= m; i++)
        sum += i;

    return sum;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM