輸入:兩個有序數組 java
輸出:兩個有序數組合併後的新有序數組 code
例:in:{2,4,5,20} class
{1,2,3,6,7,8,9,19} static
out:1 2 2 3 4 5 6 7 8 9 19 20 while
public class YXHB { public void YH(int[]a,int[]b){ int i=0,j=0,k=0; int[]NArr=new int[a.length+b.length]; while(i<a.length&&j<b.length){ if(a[i]<b[j]){ NArr[k]=a[i]; i++; k++; } else{ NArr[k]=b[j]; j++; k++; } } while(i<a.length){ NArr[k]=a[i]; i++; k++; } while(j<b.length){ NArr[k]=b[j]; j++; k++; } for(int ij=0;ij<NArr.length;ij++){ System.out.print(NArr[ij]+" "); } } public static void main(String[] args){ int[]aa={2,4,5,20}; int[]bb={1,2,3,6,7,8,9,124}; YXHB bing=new YXHB(); bing.YH(aa,bb); } }