作者donotworryok (BEN)
看板C_and_CPP
標題[問題] 有關於c語言陣列相同元素去除問題
時間Wed Mar 26 23:37:34 2014
開發平台(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
推 yerx:i=0, j=0不印, j=1時印出 break, 要把全部的j都判斷過 03/26 23:46
推 johnjohnlin:C++ unique 可以用 03/27 00:07
→ johnjohnlin:www.cplusplus.com/reference/algorithm/unique 03/27 00:07
推 dirkc:內層for邏輯不對,要比較每個j;另外同陣列裡的重複未處理 03/27 09:03