数组旋转问题 – 无法获得所需输出

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

Array rotation problem - not able to get desired output

问题

以下是您提供的代码的修改后的翻译:

public static void rotate(int a[], int d, int n) {
    int temp[] = new int[n];
    for (int i = d; i < a.length; i++) {
        for (int j = 0; j < n - 2; j++) {
            temp[j] = a[i];
        }
    }
    for (int i = 0; i < d; i++) {
        for (int j = n - 2; j < temp.length; j++) {
            temp[j] = a[i];
        }
    }
    System.out.println("结果为:");
    for (int k = 0; k < temp.length; k++) {
        System.out.println(temp[k]);
    }
}
public static void main(String[] args) {
    int size, d;
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入数组大小:");
    size = sc.nextInt();
    System.out.println("请输入旋转次数:");
    d = sc.nextInt();
    int a[] = new int[size];
    for (int i = 0; i < size; i++) {
        a[i] = sc.nextInt();
    }
    rotate(a, d, size);
}

输入

请输入数组大小 7

请输入旋转次数 2

输入数组 a[]=1 2 3 4 5 6 7

我得到的输出

7 7 7 7 7 2 2

预期的输出

3 4 5 6 7 1 2

英文:

I wrote a code to rotate an array of size n by d elements. I used temp array to solve the problem. But I am getting different output.There is syntax error in my code. Can anyone help me?
Code:

public static void rotate(int a[], int d, int n) {
    int temp[] = new int[n];
    for (int i = d; i &lt; a.length; i++) {
        for (int j = 0; j &lt; n - 2; j++) {
            temp[j] = a[i];
        }
    }
    for (int i = 0; i &lt; d; i++) {
        for (int j = n - 2; j &lt; temp.length; j++) {
            temp[j] = a[i];
        }
    }
    System.out.println(&quot;result is&quot;);
    for (int k = 0; k &lt; temp.length; k++) {
        System.out.println(temp[k]);
    }
}
public static void main(String[] args) {
    int size, d;
    Scanner sc = new Scanner(System.in);
    System.out.println(&quot;enter size&quot;);
    size = sc.nextInt();
    System.out.println(&quot;enter rotate count&quot;);
    d = sc.nextInt();
    int a[] = new int[size];
    for (int i = 0; i &lt; size; i++) {
        a[i] = sc.nextInt();
    }
    rotate(a, d, size);
}

Input:

> enter size 7
>
> enter rotate count 2
>
> input array a[]=1 2 3 4 5 6 7

Output I got:

> 7 7 7 7 7 2 2

Expected output:

> 3 4 5 6 7 1 2

答案1

得分: 1

Your logic is incorrect, you should not have double loops anywhere, they break everything and there is absolutely no reason to have them logically.

for(int i=d;i&lt;a.length;i++){
    for(int j=0;j&lt;n-2;j++){
        temp[j]=a[i];
    }
}

What this does is that every element in temp will have the value of a[a.length - 1]. All iterations of the outer loops will be completely overwritten by the last iteration of the outer loop, at which point i will be a.length - 1. Then you iterate over most of the temp and overwrite all their values with a[a.length - 1]. You misuse i and j, and the -2 should probably be -d if anything.

There are generally two ways to do this: manually wrapping the index around the array bounds or using the modulo operator to do the wrapping for you. Using modulo is the cleaner approach, and I will therefore show that one:

public static void rotate(int a[], int rotate){
    int temp[]=new int[a.length];
    for (int i = 0; i &lt; a.length; i++) {
        temp[i] = a[(i + rotate) % a.length];
    }
    
    System.out.println("result is");
    for (int k = 0; k &lt; temp.length; k++){
        System.out.println(temp[k]);
    }
}

which you then call via rotate(a,d);

英文:

Your logic is incorrect, you should not have double loops anywhere, they break everything and there is absolutely no reason to have them logically.

for(int i=d;i&lt;a.length;i++){
    for(int j=0;j&lt;n-2;j++){
        temp[j]=a[i];
    }
}

What this is does is that every element in temp will have the value of a[a.length - 1]. All iterations of the outer loops will be completely overwritten by the last iteration of the outer loop at which point i will be a.length - 1, then you iterate over most of the temp and overwrite all their values with a[a.length - 1]. You misuse i and j and the -2 should probably be -d if anything.

There are generally two ways do to this: manually wrapping the index around the array bounds or using the modulo operator to do the wrapping for you. Using modulo is the cleaner approach and I will therefore show that one:

public static void rotate(int a[], int rotate){
    int temp[]=new int[a.length];
    for (int i = 0; i &lt; a.length; i++) {
        temp[i] = a[(i + rotate) % a.length];
    }
    
    System.out.println(&quot;result is&quot;);
    for (int k = 0; k &lt; temp.length; k++){
        System.out.println(temp[k]);
    }
}

which you then call via rotate(a,d);

答案2

得分: 1

你可以通过使用一个 for循环 和一个 if语句 来简化你的代码。将较远的元素移到数组的开头,将较近的元素移到数组的末尾:

public static void rotate(int[] arr, int d) {
    int[] temp = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        if (i < arr.length - d) {
            // 将较远的元素移到数组开头
            temp[i] = arr[i + d];
        } else {
            // 将较近的元素移到数组末尾
            temp[i] = arr[i + d - arr.length];
        }
    }
    // 用临时数组的内容替换数组的内容
    System.arraycopy(temp, 0, arr, 0, arr.length);
}
public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5, 6, 7};
    rotate(arr, 2);
    System.out.println(Arrays.toString(arr));
    // [3, 4, 5, 6, 7, 1, 2]
}

<sup>另请参阅:仅使用一个分号在Java中旋转int数组</sup>

英文:

You can simplify your code by using one for loop and one if statement. Shift the far elements to the beginning of the array and shift the near elements to the end of the array:

public static void rotate(int[] arr, int d) {
    int[] temp = new int[arr.length];
    for (int i = 0; i &lt; arr.length; i++) {
        if (i &lt; arr.length - d) {
            // shift the far elements to
            // the beginning of the array
            temp[i] = arr[i + d];
        } else {
            // shift the near elements
            // to the end of the array:
            temp[i] = arr[i + d - arr.length];
        }
    }
    // replace the contents of the array
    // with the contents of the temp array
    System.arraycopy(temp, 0, arr, 0, arr.length);
}
public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5, 6, 7};
    rotate(arr, 2);
    System.out.println(Arrays.toString(arr));
    // [3, 4, 5, 6, 7, 1, 2]
}

<sup>See also: Rotating an int Array in Java using only one semicolon</sup>

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

发表评论

匿名网友

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

确定