如何循环地对二维数组中的所有元素进行求和?

huangapple go评论87阅读模式
英文:

How do you add all elements in an 2d array, circularly?

问题

给定一个二维字符串数组,如何将所有元素相加,使数组中的每个索引都是数组其余部分的和呢?例如,对于数组[[a], [b], [c], [d], [e]],最终结果是
[[a+b+c+d+e], [a+b+c+d+e], [a+b+c+d+e], [a+b+c+d+e],[a+b+c+d+e]]。我猜不一定要是二维数组,也可以是一维字符串数组,最终每个索引都是字符串"a+b+c+d+e"。当我说要循环时,我是指使用循环,不使用额外的内存空间。因此,也许在第一次迭代之后,它可能会看起来像[[a], [a + b], [a+b+c], [a+b+c+d], [a+b+c+d+e]]。实现这样的代码是什么样的呢?答案可以是伪代码或任何编程语言。

类似这样的链接:https://andrew.gibiansky.com/blog/machine-learning/baidu-allreduce/

英文:

Given an 2d array of strings how to I add up all the elements so that each index in the array is the sum of the rest of the array? For example with an array [[a], [b], [c], [d], [e]] I end up with
[[a+b+c+d+e], [a+b+c+d+e], [a+b+c+d+e], [a+b+c+d+e],[a+b+c+d+e]]. I guess it doesn't have to be a 2d array it can be a 1d array of string and I end up with a string "a+b+c+d+e" in every index. When I say circularly I mean with loops, no extra memory space. So maybe after the first iteration it could look something like [[a], [a + b], [a+b+c], [a+b+c+d], [a+b+c+d+e]] . What would the code for something like this be? The answer can be is psuedocode or any language

something like this: https://andrew.gibiansky.com/blog/machine-learning/baidu-allreduce/

答案1

得分: 1

我不太确定您想要实现什么。但是,如果您查看Arrays#parallelPrefix,您可以使用它来累积数组的单个元素。

public static void main(String[] args) {
    String[] str = {"a","b","c","d","e"};
    Arrays.parallelPrefix(str, (s1,s2) -> s1+s2);
    System.out.println(Arrays.toString(str));
}

//[a, ab, abc, abcd, abcde]
英文:

I'm not quite sure what you want to achieve. But it might be worthwhile if you look at Arrays#parallelPrefix , which you can use to cumulate the single elements of an array.

public static void main(String[] args) {
    String[] str = {"a","b","c","d","e"};
    Arrays.parallelPrefix(str, (s1,s2) -> s1+s2);
    System.out.println(Arrays.toString(str));
}

//[a, ab, abc, abcd, abcde]

答案2

得分: 1

你可以通过将所有元素添加到其中一个项目中,然后将结果复制到其他项目中来获得这样的字符串。

String[] arr = new String[] {"a", "b", "c", "d", "e"};

for(int i = 1; i < arr.length; ++i) {
    arr[0] += arr[i];
}
for(int i = 1; i < arr.length; ++i) {
    arr[i] = arr[0];
}        
System.out.println(Arrays.toString(arr));
英文:

You can get such string by adding all elements to one of the items and then copying the result to other items.

    String[] arr = new String[] {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;};
	
	for(int i = 1; i &lt; arr.length; ++i) {
		arr[0] += arr[i];
	}
	for(int i = 1; i &lt; arr.length; ++i) {
		arr[i] = arr[0];
	}		
	System.out.println(Arrays.toString(arr));

答案3

得分: 1

根据您附上的链接,我认为以下解决方案会对您有所帮助:

int n = 5, i, j;
String[][] gpu = new String[n][n];
// 在此生成初始 GPU 数组...
for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
        char ch = (char) (97 + j);
        gpu[i][j] = "" + ch + i;
    }
}
System.out.println("初始 GPU 数组:");
for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
        System.out.print(gpu[i][j] + "|");
    }
    System.out.println();
}
// 在此计算您期望的逻辑
for (i = 0; i < n-1; i++) {
    for (j = 0; j < n; j++) {
        gpu[(i+j+1)%n][j] = gpu[(i+j)%n][j] + gpu[(i+j+1)%n][j];
    }
}
System.out.println("最终 GPU 数组:");
for (i = 0; i < n; i++) {
    System.out.print(gpu[n-1][i] + "|");
}
System.out.println();
英文:

According to your attached link i think below solution will help you:

        int n = 5, i, j;
        String[][] gpu = new String[n][n];
        //here generate initial gpu....
        for (i = 0; i &lt; n; i++) {
            for (j = 0; j &lt; n; j++) {
                char ch = (char) (97 + j);
                gpu[i][j] = &quot;&quot;+ch+i;
            }
        }
        System.out.println(&quot;Initial Gpu: &quot;);
        for (i = 0; i &lt; n; i++) {
            for (j = 0; j &lt; n; j++) {
                System.out.print(gpu[i][j] + &quot;|&quot;);
            }
            System.out.println();
        }
        // here calculated your expected logic
        for (i = 0; i &lt; n-1; i++) {
            for (j = 0; j &lt; n; j++) {
                gpu[(i+j+1)%n][j] = gpu[(i+j)%n][j] + gpu[(i+j+1)%n][j];
            }
        }
        System.out.println(&quot;Final Gpu: &quot;);
        for (i = 0; i &lt; n; i++) {
            System.out.print(gpu[n-1][i] + &quot;|&quot;);
        }
        System.out.println();

huangapple
  • 本文由 发表于 2020年5月3日 18:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61573257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定