2012-12-01 280 views
2
int [] queue1 = {4,7,2,9,12,35,8,49}; 
    int [] queue2 = {24,53,6,19,41,71,1,68,11,32,99}   
    int[]mergeQ = new int[queue1.length + queue2.length]; 

    for(int i=0; i < queue1.length; i++) 
    { 

     mergeQ[i*2] = queue1[i];  
     mergeQ[i*2+1] = queue2[i]; 
    } 
    for(int i=0; i < mergeQ.length; i++) {    
     System.out.print(mergeQ[i]+","); 
    } 

輸出:4,24,7,53,2,6,9,19,12,41,35,71,8,1,49,68,0,0,0將兩個數組合併爲一個

我怎樣才能打印出隊列2的其他元素?

+2

你沒有把在所有數字在'queue2',所以'mergeQ的端部'是0. – nhahtdh

回答

6

您的合併算法假設queue1.length < queue2.length。雖然它對你的程序是正確的,但做出這樣的假設通常不是一件好事。

將合併算法更改爲遍歷兩個數組,直到您碰到較短數組的長度,然後將較長數組的其餘元素轉儲到合併數組的尾部。你可以做到這一切在一個單一的循環,就像這樣:

int p = 0; 
for (int i = 0 ; i < queue1.length || i < queue2.length ; i++) { 
    if (i < queue1.length) { 
     mergeQ[p++] = queue1[i]; 
    } 
    if (i < queue2.length) { 
     mergeQ[p++] = queue2[i]; 
    } 
} 
+0

它看起來很簡單....我很愚蠢。十分感謝。 – johnnily

0

雖然休閒代碼會將所有元素放置在合併隊列中,但我不確定是否有任何您希望合併的兩個隊列的順序(例如隊列一中的元素與隊列二中的元素一起休耕)。

int [] 
    queue1 = {4,7,2,9,12,35,8,49}, 
    queue2 = {24,53,6,19,41,71,1,68,11,32,99}, 
    mergeQ = new int[queue1.length + queue2.length]; 

for (int i=0; i < queue1.length; i++) 
    mergeQ[i] = queue1[i]; 

for (int i = 0; i < queue2.length; i++) 
    mergeQ[queue1.length + i] = queue2[i]; 

for(int i=0; i < mergeQ.length; i++) 
    System.out.print(mergeQ[i]+","); 
+0

現在我覺得愚蠢的不問關於訂單的問題....我同意...這是一個更直接的方法 – IndianNoob

+0

該代碼只複製e由於'mergeQ [queue1.length + i] = queue1 [i]'中的錯誤而引起隊列1的錯誤;' –

+0

已修復。抱歉。這是一個簡單的錯字。 – Dawson

1

下面是做到這一點的一種方法:

int[] queue1 = { 4, 7, 2, 9, 12, 35, 8, 49 }; 
    int[] queue2 = { 24, 53, 6, 19, 41, 71, 1, 68, 11, 32, 99 }; 
    int[] mergeQ = new int[queue1.length + queue2.length]; 

    int dest = 0; 
    int src1 = 0; 
    int src2 = 0; 
    while (src1 < queue1.length || src2 < queue2.length) { 
     if (src1 < queue1.length) { 
      mergeQ[dest++] = queue1[src1++]; 
     } 
     if (src2 < queue2.length) { 
      mergeQ[dest++] = queue2[src2++]; 
     } 
    } 

    for (int i = 0; i < mergeQ.length; i++) { 
     System.out.print(mergeQ[i] + ","); 
    } 

這個工程無論queue1queue2是否較短。

它還保留了算法的屬性,即兩個隊列的元素是交錯的。如果這不重要,那麼整個事情可以被兩個電話System.arraycopy()取代。

0
int[] queue1 = {4,7,2,9,12,35,8,49}; 
int[] queue2 = {24,53,6,19,41,71,1,68,11,32,99}   
int[] mergeQ = new int[queue1.length + queue2.length 

int i=0; 
for(; i < queue1.length; i++){ 
    mergeQ[i*2] = queue1[i];  
    mergeQ[i*2+1] = queue2[i]; 
} 
if(queue1.length>queue2.length){ 
for(int j=i;j<queue1.length;j++) 
    mergeQ[j+2] = queue1[j]; 
}else if(queue1.length<queue2.length){ 
for(int j=i;j<queue2.length;j++) 
    mergeQ[j+2] = queue2[j]; 
} 
0

檢查哪個數組更長。保留兩者之間的差異的計數器。更改代碼有點...多一點非正統......卻多了幾分乾淨

int [] queue2 = {24,53,6,19,41,71,1,68,11,32,99}; 

int[]mergeQ = new int[queue1.length + queue2.length]; 

int larger=queue1.length; 
int smaller=queue2.length; 

if(queue1.length < queue2.length) 
    { 
    larger=queue2.length; 
    smaller=queue1.length; 
    } 

    for(int i=0; i < queue1.length; i++) 
    { 
     mergeQ[i*2] = queue1[i];  
     mergeQ[i*2+1] = queue2[i]; 

    } 

    for(int i=mergeQ.length; i < queue2.length; i++) 
    { 
     mergeQ[i] = queue2[larger-smaller]; 
     smaller++; 
    } 

    for(int i=0; i < mergeQ.length; i++){ 


     System.out.print(mergeQ[i]+","); 
    } 

這應該做到這一點,你

0

輸入:{1,3,5,7}和{ 2,4,6} 輸出:{1,2,3,4,5,6,7}}

 int c[] = {2, 4, 6, 8}; 
     int n[] = {1, 3, 5, 7, 9}; 
     int f = 0; 
     int res[] = new int[c.length + n.length]; 
     for(int i=0;i<(c.length + n.length); i++) 
     { 
      if(c.length > n.length) 
      { 
       if(i<c.length) 
        res[f++] = c[i]; 
       else if(f<=res.length-1) 
        res[f++] = c[i]; 

       if(i<n.length) 
       res[f++] = (char) n[i]; 
      }else{ 
       if(i<n.length) 
        res[f++] = n[i]; 
       else if(f<=res.length-1) 
        res[f++] = n[i]; 

       if(i<c.length) 
       res[f++] = (char) c[i]; 
      } 

     } 
     for(int i=0;i<res.length;i++) 
     { 
      System.out.print(" "+res[i]); 
     } 
    } 
} 
+0

不鼓勵代碼傾銷。對你所做的一些解釋會使OP受益 - 特別是如果他或她是初學者的話。解釋也不鼓勵簡單的複製和粘貼,不理解你做了什麼。 – rayryeng