猿问

如何让所有偶数先出现,然后奇数出现?

我遇到的问题是如何重新排列 int 值数组,以便所有偶数值出现在所有奇数值之前。下面是我的编码:


   import java.io.*; 


  public class EvenAppearBeforeOdd {

// function to rearrange the array in given way. 

static void rearrangeEvenAndOdd(int arr[], int n) 

    // variables 

    int j = -1,temp; 


    // quick sort method 

    for (int i = 0; i < n; i++) { 


        // if array of element 

        // is odd then swap 

        if (arr[i] % 2 == 0) { 


            // increment j by one 

            j++; 


            // swap the element 

            temp = arr[i]; 

            arr[i] = arr[j]; 

            arr[j] = temp; 

        } 

    } 


// Driver code 

public static void main(String args[]) 

    int arr[] = { 15, 9, 1, 3, 10, 5, 4, 8 }; 

    System.out.println("\n\nBefore\n");

    System.out.println(arr[]);

     try {

        System.out.println(EvenAppearBeforeOdd.rearrangeEvenAndOdd(arr);

    } catch (Exception e) {

        System.out.println("Error!!!");

   }

    int n = arr.length; 


    rearrangeEvenAndOdd(arr, n); 

  System.out.println("\n\nAfter\n");

    for (int i = 0; i < n; i++) 

        System.out.print(arr[i] + " "); 

  } 

错误输出如下所示:


  run:

  C:\Users\User\AppData\Local\NetBeans\Cache\8.0.2\executor-snippets\run.xml:48: 

  Cancelled by user.

  BUILD FAILED (total time: 3 seconds)

实际上我想要如下的输出:


  run:



  Before


  15 9 1 3 10 5 4 8 



  After


  10 4 8 3 15 5 9 1 BUILD SUCCESSFUL (total time: 0 seconds)

希望有人能帮我检查一下我的代码哪部分错了?多谢。


慕哥6287543
浏览 125回答 3
3回答

holdtom

下面给出的是产生与示例输入和输出相匹配的结果的答案:public class EvenAppearBeforeOdd {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; static void rearrangeEvenAndOdd(int arr[]) {&nbsp; &nbsp; &nbsp; &nbsp; int j = 0, temp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = arr[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j] = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static void printArray(int arr[]) {&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<arr.length;i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(arr[i]+"\t");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; int arr[] = { 15, 9, 1, 3, 10, 5, 4, 8 };&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Before:");&nbsp; &nbsp; &nbsp; &nbsp; printArray(arr);&nbsp; &nbsp; &nbsp; &nbsp; rearrangeEvenAndOdd(arr);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("After:");&nbsp; &nbsp; &nbsp; &nbsp; printArray(arr);&nbsp; &nbsp; }}你可以将你的答案与它进行比较,很容易找到错误。如果您仍有任何问题,请随时发表评论。更新 [2019 年 10 月 6 日 19:00]:为了解决WJS提出的问题,rearrangeEvenAndOdd方法可以写成:static void rearrangeEvenAndOdd(int arr[]) {&nbsp; &nbsp; int j, temp;&nbsp; &nbsp; for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j=i-1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(j>=0 && arr[j]%2==1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j+1]=arr[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j+1]=temp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}但是,它不会生成与示例输入和输出匹配的结果,即输入数组为 { 15, 9, 1, 3, 10, 5, 4, 8 },结果将生成为 {10, 4, 8 , 15, 9, 1, 3, 5} 而不是您的示例输出,即 {10, 4, 8, 3, 15, 5, 9, 1}。

偶然的你

如果您必须在算法中不使用任何额外的内存,那么我们必须找到一种方法来修复您当前的方法。我认为概念上最简单的方法是使用重复的空数组来构建输出。首先,迭代输入数组并复制所有偶数值。然后,再次迭代并复制所有奇数值。static int[] rearrangeEvenAndOdd(int arr[], int n) {&nbsp; &nbsp; int[] output = new int[n];&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; for (int i=0; i < n; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] % 2 == 0) output[count++] = arr[i];&nbsp; &nbsp; }&nbsp; &nbsp; for (int i=0; i < n; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] % 2 == 1) output[count++] = arr[i];&nbsp; &nbsp; }&nbsp; &nbsp; return output;}public static void main (String[] args) {&nbsp; &nbsp; int[] arr = { 15, 9, 1, 3, 10, 5, 4, 8 };&nbsp; &nbsp; System.out.println(Arrays.toString(arr));&nbsp; &nbsp; arr = rearrangeEvenAndOdd(arr, arr.length);&nbsp; &nbsp; System.out.println(Arrays.toString(arr));}这打印:[15, 9, 1, 3, 10, 5, 4, 8][10, 4, 8, 15, 9, 1, 3, 5]请注意,这是一个线性解决方案,需要对输入数组进行两次完整扫描,并且还需要两倍的输入存储空间。

萧十郎

你的语法似乎错误。不确定它是否是勘误表,但您应该尝试在编辑器(eclipse)中进行编译。它将显示有关语法的所有错误和警告。System.out.println(arr[]);System.out.println(EvenAppearBeforeOdd.rearrangeEvenAndOdd(arr);
随时随地看视频慕课网APP

相关分类

Java
我要回答