Java 数组的累积和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24335660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:36:18  来源:igfitidea点击:

Cumulative sum of an Array

javaarraysloopsfor-loop

提问by

So i'm working on a problem that focuses on taking the cumulative sum of an array so for example if i have an array of ({0,2,3,-1,-1}) it returns {0,2,5,4,3}... or if you have an array of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] it should return [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]...

所以我正在研究一个专注于获取数组的累积和的问题,例如,如果我有一个 ({0,2,3,-1,-1}) 数组,它返回 {0,2,5 ,4,3}... 或者如果你有一个 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 数组,它应该返回 [1, 3, 6, 10, 15, 21、28、36、45、55]...

Right now i'm struggling with two problems one i have to use the method given and i'm struggling with what to return because total will not.. To my code i know it works for adding up the sum of an array but not the cumulative sum as in my examples.. any guidelines would be helpful.

现在我正在努力解决两个问题,一个我必须使用给定的方法,我正在努力返回什么,因为 total 不会......在我的例子中的累积总和......任何指导方针都会有所帮助。

public int[] makeCumul(int[] in) {
    int[] out = { in.length };
    int total = 0;
    for (int i = 0; i < out.length; i++) {
        total += out[i];
    }
    return total;
}

采纳答案by Jaaz Cole

Not reading the in array, partly, but also not updating the out array, and not returning it. This should work for you.

部分不读取 in 数组,但也不更新 out 数组,也不返回它。这应该对你有用。

public int[] makeCumul(int[] in) {
    int[] out = new int[in.length];
    int total = 0;
    for (int i = 0; i < in.length; i++) {
        total += in[i];
        out[i] = total;
    }
    return out;
}

回答by Mike Elofson

public static int[] makeCumul(int[] in) {
    int[] out = new int[in.length];
    int sum = 0;
    for(int i = 0; i < in.length; i++){
        sum += in[i];
        out[i] = sum;
    }
    return out;
}

I believe this is what you are looking for. Keep a cumulative sum, and update that sum with each element. After you update the sum, replace the element with the sum.

我相信这就是你正在寻找的。保留累积总和,并使用每个元素更新该总和。更新总和后,用总和替换元素。

When you create a new array, to initialize it use

当你创建一个新数组时,初始化它使用

int[] out = new int[ARRAY SIZE HERE];

You should also note that in the method signature you are returning an array of integers, and the variable totalis an integer, not an array of integers. So you want to return the variable out.

您还应该注意,在方法签名中您返回的total是一个整数数组,而变量是一个整数,而不是一个整数数组。所以你想返回变量out

Gives me the output:

给我输出:

0
2
5
4
3

回答by CMPS

public class Sum {
    public static void main(String[] args) {
        int in[] = {1,2,3,4,5,6,7,8,9};
        int[] out = new int[in.length];
        out[0] = in[0];
        for (int i = 1; i < out.length; i++) 
            out[i] = out[i-1] + in[i];

        for (int i = 0; i < out.length; i++) 
            System.out.print(out[i]+" ");
    }
}

OUTPUT

输出

1 3 6 10 15 21 28 36 45 

If you want to put this in a method, you can return the last element like this:

如果你想把它放在一个方法中,你可以像这样返回最后一个元素:

return out[out.length-1];