将两个阵列组合成一个交替元素?

时间:2016-02-07 17:35:32

标签: java

我一直在尝试使用以下算法解决问题,但它不起作用。

import java.util.Scanner;
import java.lang.Math;

public class AlterConcatenateArrays {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        System.out.println("How many elements do you want the first array have?");
        int N = s.nextInt();
        s.nextLine();
        System.out.println("How many elements do you want the second array have?");
        int M = s.nextInt();
        int[] a = new int[N];
        int[] b = new int[M];
        System.out.println("The elements of the first array are: ");
        for (int i = 0; i < N; i++) {
            a[i] = (int) (Math.random() * 20);
            System.out.print(a[i] + " \n");
        }
        System.out.println("The elements of the second array are: ");
        for (int i = 0; i < M; i++) {
            b[i] = (int) (Math.random() * 20);
            System.out.print(b[i] + " \n");
        }
        System.out.println("Now we are going to concatenate the arrays by alternatingly choosing");
        int[] c = new int[N + M];
        for (int i = 0; i < ((N + M) / 2); i++) {
            a[i] = c[2 * i + 0];
            b[i] = c[2 * i + 1];
        }
        System.out.println("The new array is: ");
        for (int i = 0; i < N + M; i++) {
            System.out.print(c[i] + "\t");
        }
    }
}

这个程序的输出是:

How many elements do you want the first array have?
2
How many elements do you want the second array have?
3
The elements of the first array are: 
17 
18 
The elements of the second array are: 
6 
14 
15 
Now we are going to concatenate the arrays by alternatingly choosing
The new array is: 
0   0   0   0   0   

4 个答案:

答案 0 :(得分:3)

你在这里交换了作业。

{
    a[i] = c[2*i+0];
    b[i] = c[2*i+1];
}

应从ca分配到b

{
    c[2*i+0] = a[i];
    c[2*i+1] = b[i];
}

答案 1 :(得分:0)

记住L值和R值之间的差异。 在编程中一直很重要

module.factory("AuthorizationsService", ["$q",
  function($q) {

   var checkPermission = function {
     // check and set the permissions from http service
     // return a promise
   };

   return {
     checkPermission: checkPermission
   }
}]);

这是错误的

答案 2 :(得分:0)

Hello zh c数组隐式初始化为零。在for循环中,我认为,你想要填充c数组,而不是a和b。

答案 3 :(得分:0)

//编程合并2个相同或不同长度的数组的替代元素

public class MergeAlternateElements {
public static void mergeAlternateElements(int[] a, int[] b) {
    int n = a.length, m = b.length;
    int mergedArray[] = new int[n + m];
    int p = 0;
    int i = 0;
    for (i = 0; i < n; i++) {
        mergedArray[p] = a[i];
        p++;
        if (i < m) {
            mergedArray[p] = b[i];
            p++;
        }
    }
    if (n < m) {
        System.arraycopy(b, i, mergedArray, p, m - n);
    }
    System.out.println("Merged Array:");
    for (int k : mergedArray)
        System.out.print(k + " ");

}

public static void main(String[] args) {
    int[] a = { 24, 2, 45, 20, 56, 75, 21, 56, 99, 53 };
    int b[] = { 112, 234, 500, 123, 432, 567, 787, 909, 808, 600, 678, 900};
    mergeAlternateElements(b, a);
    System.out.println();
    mergeAlternateElements(a, b);
 }
}
相关问题