Re: [問題] 判斷輸出問題

作者: pziyout (pziyout)   2014-08-18 11:28:31
※ 引述《oklp1415 (天生我材)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: Dev-C++
: 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
: 問題(Question):
: 餵入的資料(Input):
: 預期的正確結果(Expected Output):
: 錯誤結果(Wrong Output):
: 程式碼(Code):(請善用置底文網頁, 記得排版)
: ....先前一些零粹的code就不PO上來了
: 以下是我要判斷的條件輸出程式碼
: int q[5];
: for(c=0,d=0;c<5;c++){
: for(e=0;e<5;e++){
: if(a[c]!=a[e]){
: q[c]=a[c];
: }
: for(int j=0;j<5;j++){
: printf("%d",q[j]);
: }
: }
: }
: }
: 比如說輸入一串數字 1 2 3 4 5 1 2 3 5 6
: 輸出會是 1 2 3 4 5 6
: 意思把前面重複的數字刪除
: 我做法是每往下一個,判斷後重頭再跑一次判斷,額外存入另一個變數內
: 最後依序輸出不知道這樣做法哪裡錯誤了~"~一直弄不出來
: 求解~.~thanks
: 補充說明(Supplement):
這種問題若以 C++ 的作法,我會這樣做:
string foo("1 2 3 4 5 1 2 3 5 6") ;
istringstream istr(foo) ;
int n ;
set<int> seen ;
while ( istr >> n ) {
if ( seen.find(n) == seen.end() ) {
cout << n << " " ;
seen.insert(n) ;
}
}
cout << endl ;
以上假設輸入的值已經存到一個字串內,此字串當然可用 getline 取得。
執行效率上可能稍慢,不過程式易懂。

Links booklink

Contact Us: admin [ a t ] ucptt.com