循环遍历数组以对值进行分组,计算总和。

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

loop an array to group values for a total

问题

以下是翻译好的内容:

import java.util.Arrays;

public class arrayAdding {

    public static void main(String[] args) {
        int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6 };
        int[] newArrNumbers = new int[6];

        for (int i = 0; i < arrNumbers.length; i++) {
            newArrNumbers[i < 6 ? i : (6 - (i % 6) - 1)] += arrNumbers[i];
        }
        System.out.println(Arrays.toString(newArrNumbers));
    }
}

实际代码结果:

newArrNumbers = [13, 12, 11, 10, 9, 8]

所需结果:

newArrNumbers = [8, 9, 10, 11, 12, 13]
英文:

How can I write a for loop that goes through an array and splits it into groups of 6. From there it should add the first group in the arrNumbers, then add the second group but reversed so { 6, 5, 4, 3, 2, 1 } to first group which is { 1, 2, 3, 4, 5, 6 } and then third group should be not reversed, but then if there is another group of 6, the set should be reversed to add. I don't know how to go about this. Any help will be appreciated. Below is my attempt

import java.util.Arrays;

public class arrayAdding{

     public static void main(String []args){
        int[] arrNumbers = new int[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6 };
        int[] newArrNumbers = new int[6];
        
        for(int i  = 0; i &lt; arrNumbers.length ; i++){
            newArrNumbers[i &lt; 6 ? i : (6 - (i % 6) - 1)] += arrNumbers[i];
        }
        System.out.println(Arrays.toString(newArrNumbers));
     }
}

Actual Result from my code:

newArrNumbers = [13, 12, 11, 10, 9, 8]

The required result should be:

newArrNumbers = [8, 9, 10, 11, 12, 13]

答案1

得分: 2

以下是翻译好的内容:

你可以将其视为由12个元素组成的群体,前6个元素不翻转,后6个元素翻转。

  • (i % 12) < 6 - 前半部分 - i % 6
  • (i % 12) >= 6 - 后半部分(翻转) - (6 - (i % 6) - 1)

代码:

newArrNumbers[(i % 12) < 6 ? i % 6 : (6 - (i % 6) - 1)] += arrNumbers[i];
英文:

You can think like group of 12 elements and not reversed for the first 6 and reversed for the next 6.

  • (i % 12) &lt; 6 - First half - i % 6
  • (i % 12) &gt;= 6 - Second half(reversed) - (6 - (i % 6) - 1)

Code:

newArrNumbers[(i % 12) &lt; 6 ? i % 6 : (6 - (i % 6) - 1)] += arrNumbers[i];

答案2

得分: 1

import java.util.*;

public class ArrayAdding {

    public static void main(String []args){
        Integer[] arrNumbers = new Integer[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6 };
        int[] newArrNumbers = new int[6];
        List<List<Integer>> partitions = ArrayAdding.splitArray(arrNumbers);

        Iterator<List<Integer>> itr = partitions.iterator();
        while (itr.hasNext()) {
            List<Integer> next = itr.next();
            if (next.size() < 6 )
                itr.remove();
        }

        for(int i  = 0; i < partitions.size() ; i++){
            if (i % 2 != 0) {
                Collections.reverse(partitions.get(i));
            }
        }

        for (List<Integer> partition : partitions) {
            for (int j = 0; j < 6; j++) {
                newArrNumbers[j] = partition.get(j) + newArrNumbers[j];
            }
        }
        System.out.println(Arrays.toString(newArrNumbers));
    }

    private static List<List<Integer>> splitArray(Integer[] arrNumbers) {
        int partitionSize = 6;
        List<List<Integer>> partitions = new LinkedList<>();
        List<Integer> originalList = Arrays.asList(arrNumbers);
        for (int i = 0; i < originalList.size(); i += partitionSize) {
            partitions.add(originalList.subList(i,
                    Math.min(i + partitionSize, originalList.size())));
        }
        return partitions;
    }
}
英文:

You can refer below code:

import java.util.*;

public class ArrayAdding {

public static void main(String []args){
Integer[] arrNumbers = new Integer[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6 };
int[] newArrNumbers = new int[6];
List&lt;List&lt;Integer&gt;&gt; partitions = ArrayAdding.splitArray(arrNumbers);
Iterator&lt;List&lt;Integer&gt;&gt; itr = partitions.iterator();
while (itr.hasNext()) {
List&lt;Integer&gt; next = itr.next();
if (next.size() &lt; 6 )
itr.remove();
}
for(int i  = 0; i &lt; partitions.size() ; i++){
if (i % 2 != 0) {
Collections.reverse(partitions.get(i));
}
}
for (List&lt;Integer&gt; partition : partitions) {
for (int j = 0; j &lt; 6; j++) {
newArrNumbers[j] = partition.get(j) + newArrNumbers[j];
}
}
System.out.println(Arrays.toString(newArrNumbers));
}
private static List&lt;List&lt;Integer&gt;&gt; splitArray(Integer[] arrNumbers) {
int partitionSize = 6;
List&lt;List&lt;Integer&gt;&gt; partitions = new LinkedList&lt;&gt;();
List&lt;Integer&gt; originalList = Arrays.asList(arrNumbers);
for (int i = 0; i &lt; originalList.size(); i += partitionSize) {
partitions.add(originalList.subList(i,
Math.min(i + partitionSize, originalList.size())));
}
return partitions;
}}

huangapple
  • 本文由 发表于 2020年9月7日 18:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63775998.html
匿名

发表评论

匿名网友

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

确定