移动数组元素

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

Shift elements of an array

问题

以下是代码的翻译部分:

我正在编写一个将所有偶数移动到数组的前面将奇数移动到后面的代码所以当数组看起来像这样时

int[] a={1,3,2,5, 4, 7, 8, 6};

输出应该如下所示:

int[] b={2,4,8,6, 1, 3, 5, 7};

我的问题是程序应该返回一个数组,其中包含每个偶数移动到这些位置的位置。根据我的示例,输出应该如下所示: [2, 3, 4, 4]

shiftArray = {2,3,4,4};

因为数字2移动了两个位置,数字4移动了三个位置等等。

这是我目前的代码:

int temp=0;
int a=0;
for(int i=0;i<arr.length;i++){
    if(arr[i]%2==0){

        for (int j=i;j>a;j--){
            temp=arr[j-1];
            arr[j-1]=arr[j];
            arr[j]=temp;
        }
        a++;
    }
return arr;
}

如何使用我目前的代码来获得我想要的输出?

英文:

I'm writing a code that moves all even numbers to the front of the array and odd numbers to the back. So when an array looks like this:

int[] a={1,3,2,5, 4, 7, 8, 6};

the output should look like:

int[] b={2,4,8,6, 1, 3, 5, 7};

my problem is that the programme should return an array of positions that every even number moved to get to these positions. with my example the output should look like this: [2, 3, 4, 4]

shiftArray = {2,3,4,4};

as number 2 moved two positions, number 4 three positions etc.

here is what i have so far:

int temp=0;
int a=0;
for(int i=0;i&lt;arr.length;i++){
    if(arr[i]%2==0){

        for (int j=i;j&gt;a;j--){
            temp=arr[j-1];
            arr[j-1]=arr[j];
            arr[j]=temp;
        }
        a++;
    }
return arr;
}

how can i use what i have so far to get my desired output?

答案1

得分: 2

在这种情况下,“这个数字需要移动多少次”实际上只是“在这个数字之前总共有多少个奇数数字”,因为您试图将所有偶数移到开头,而不改变偶数数字的相对位置。每个偶数数字都需要“移动”它前面有多少个奇数数字。

所以你只需要保持一个累积计数:

int[] a = { 1, 3, 2, 5, 4, 7, 8, 6 };
List<Integer> shifts = new ArrayList<>();
int oddNumberCount = 0;
for (int i : a) {
    if (i % 2 == 0) {
        shifts.add(oddNumberCount);
    } else {
        oddNumberCount++;
    }
}

我不知道会有多少个偶数数字,所以我使用了一个列表。如果您需要将其输出为数组,您可以简单地将其转换为数组,就像这样(另请参见):

int[] result = shifts.stream().mapToInt(x -> x).toArray();

如果您知道将会有多少个偶数数字,当然可以直接使用给定大小的数组开始。

英文:

"How many times does this number need to shift" in this case is actually just "how many odd numbers in total are before this number", because you are trying to move all the even numbers to the beginning without changing the relative positions of the even numbers. Each even number needs to "move over" however many odd numbers there are before it.

So you just need to keep a cumulative count:

int[] a = { 1, 3, 2, 5, 4, 7, 8, 6 };
List&lt;Integer&gt; shifts = new ArrayList&lt;&gt;();
int oddNumberCount = 0;
for (int i : a) {
    if (i % 2 == 0) {
        shifts.add(oddNumberCount);
    } else {
        oddNumberCount++;
    }
}

I don't know how many even numbers there will be, so I used a list. If you require an array as the output you can convert it to a list simply like this (See Also):

int[] result = shifts.stream().mapToInt(x -&gt; x).toArray();

If you do know how many even numbers there will be, you can of course just start with an array of that given size.

答案2

得分: 0

你代码中缺少的部分是你没有正确追踪偶数的当前位置:

public static void main(String...arg){
    int[] a={1,3,2,5, 4, 7, 8, 6,0}; //要移动的数组
    
    int indexEvenNumber = 0;//如果找到偶数,我们应该把它放在哪里。
    for (int i = 0; i < a.length; i++) {
        if (a[i] % 2 == 0) {
            int temp = a[i];
            a[i] = a[indexEvenNumber];
            a[indexEvenNumber] = temp;
            indexEvenNumber++;
        }
    }
    // 打印移动后的数组以确认结果
    Arrays.stream(a).forEach(System.out::println);
}
英文:

What are you lacking in your code is you don't track what's the current position of the even numbers correctly:

public static void main(String...arg){
    int[] a={1,3,2,5, 4, 7, 8, 6,0}; //the array to be shifted
    
    int indexEvenNumber = 0;//where should we put the even number if we found it.
    for (int i = 0; i &lt; a.length; i++) {
        if (a[i] % 2 == 0) {
            int temp = a[i];
            a[i] = a[indexEvenNumber];
            a[indexEvenNumber] = temp;
            indexEvenNumber++;
        }
    }
    // print the shifted array to confirm results
    Arrays.stream(a).forEach(System.out::println);

}

huangapple
  • 本文由 发表于 2023年3月3日 21:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627626.html
匿名

发表评论

匿名网友

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

确定