[問題] 有關於c語言陣列相同元素去除問題

看板C_and_CPP作者 (BEN)時間11年前 (2014/03/26 23:37), 編輯推噓3(301)
留言4則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) c 問題(Question): 假設我現在有兩個array int array_a[5]={11,22,33,22,44}; int array_b[5]={11,22,33,22,44}; 我想要在這兩個array先去判斷不一樣的數字才會printf出來,但問題是我已經處理好 兩個array後,發現還是有重複的數字問題出來 11 22 33 22 44 改了很久還是不知道哪裡有問題,請問哪行有判斷錯誤? 餵入的資料(Input): 預期的正確結果(Expected Output): 11 22 33 44 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <string.h> int main() { int array_a[5]={11,22,33,22,44}; int array_b[5]={11,22,33,22,44}; int array_c[5]; int i,j; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(array_a[i]!=array_b[j]) { printf("%d\n",array_a[i]); break; } } } return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.165.190.53 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1395848256.A.1B2.html

03/26 23:46, , 1F
i=0, j=0不印, j=1時印出 break, 要把全部的j都判斷過
03/26 23:46, 1F

03/27 00:07, , 2F
C++ unique 可以用
03/27 00:07, 2F

03/27 00:07, , 3F
www.cplusplus.com/reference/algorithm/unique
03/27 00:07, 3F

03/27 09:03, , 4F
內層for邏輯不對,要比較每個j;另外同陣列裡的重複未處理
03/27 09:03, 4F
文章代碼(AID): #1JClH06o (C_and_CPP)