2006
04.29

一個 C 的小問題..

在 lang.program 的全國連線板,有個叫做 huaiying 的傢伙發表了一篇文章,
內容如下:

各位大大
可以幫我解決下列的題目嗎?
謝謝!!(4/28)明天就要交ㄌ~
因為我們是教C++ 所以可以用C++的答案給我嗎?
『使用一個一維陣列來解決以下問題,讀進20個10~100的整數可以重複,
當每個數讀進來時,如果之前沒有輸入過這個數,
便將他印出來。請使用『最壞的情況』,(即20個都不一樣)來測試。
盡你所能的以最小的陣列來解決這個問題』

所以我一時興起,就寫了一段程式回給他

#include<stdio.h>

int main(void)
{
    int arr[20];
    int input;
    int i,j = 0;
    for(i = 0 ; i < 20 ; i++)
    {
        printf("\n Input the number:");
        scanf("%d", &input);
        arr[i] = input;
        printf(" Input: %d ", arr[i]);
        for( j = 0 ; j < i ; j++ )
            if( input == arr[j] )
                printf(".....EXIST.");
    }
    return 0;
}

當然這個寫法還滿醜的…

不過,十幾分鐘後,有位叫做 yoco 的前輩回了一篇用 C++ 寫的

int main ( int argc, char * argv [] )
{
    set<int> s ;
    for ( int i=0; i<20; ++i ) {
        int n ; cin >> n ;
        if ( s.find(n) == s.end() ) {
            cout << n << endl ; s.insert ( n ) ;
        }
    }
}

真妙 :P

No Comment.

Add Your Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>