找回密碼
 註冊

C++ 讀取20個數字,輸出時~重複的不印出

来源: 新聞 gimball1220 2010-10-8 09:37 只看這個作者 |閱讀模式
7 13744
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 Use a one-dimensional array to solve the following problem. Read in 20 numbers from a file. As each number is read, store it in the array only if it is not a duplicate of a number already read. After reading all the values, print only the unique values into a file.
Apart from the main function, your program has to include at least one function. The input file should appear as follows:

27 -45 563 0 2 -84 27 -384 3625 -45
84 59 563 4675 62 -384 27 372 94 153

The output file should appear as follows:
27 -45 563 0 2 -84 -384 3625 84 59
4675 62 372 94 153

想先把輸入值放進x[20]
然後y[o] =x
   檢查如果有重複道就略過(continue)
但事情往往不是"攏郎"想的這麼簡單,怎麼寫不是條件不檢查就是跑出亂碼~"~
好像可以用clear,只是網路上查的資料...我看不懂-.-"
請神人指點...(別用太難的,我才剛學一個多月@@"~)


JSD-Google [/img] キキ.キキ. .. [/url]
收藏
收藏0
原 來 " 平 淡 " 也 可 以 這 麼 美 好 ! !

網友回覆7

跳到指定樓層
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 以前有寫過~但是語法都忘了
我說說看我的想法好了
你用一串語法用鎮列第一格去減之後的幾格達案先放某個暫存
之後檢查某暫存如果答案為零,就把該格陣列設零
這樣跟第一格重複的都為零
再來使用第二格去檢查(雙重回圈)
輸出的時候檢查為零者不列印
這樣應該可行~跟玩樂透的意思很像

JSD-Google [/img] :.| ..ǒǒ [/url]
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 用比較的最簡單XDDDDD
先把20個數放入x[21]裡   //我習慣從x[1]開始用
int i = 1 ;
int j = 1 ;
int k = 1 ;
int same = 0 ;
while( i<=20 )
{
     k=1 ;                //重設
     same = 0 ;                //重設
     while( k<j )        //目前要輸出的第j個數
     {
          if( x == x[k] )        //開始比...慢慢比...
          {
               same++ ;                //如果有重複變數++
               break ;
          }
          k++ ;
     }
     if( same == 0 )                //沒重複的話變數為0就輸出
     {
          cout << x << " " ;
     }
     i++ ;                //跳到下一個要比較的數
     j++ ;
}
沒跑過不知道對不對...試試看C++ 讀取20個數字,輸出時~重複的不印出4711
[ 本文章最後由 jeff77731 於 2010-10-8 10:21 編輯 ]

JSD-Google [/img] :.| ..ЖЖ [/url]
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 #include <stdio.h>
#include <stdlib.h>
#define N 20
main()
{
        int i,j,temp,a[]={27 ,-45 ,563 ,0 ,2 ,-84 ,27 ,-384 ,3625 ,-45,84 ,59 ,563 ,4675 ,62 ,-384 ,27 ,372 ,94 ,153};
        for(i=0;i<N-1;i++)
        {
                for(j=i+1;j<N;j++)
                        if(a>a[j])
                        {
                                temp=a;
                                a=a[j];
                                a[j]=temp;
                        }
        }
        for(i=0;i<N;i++)
        {
                if(i>0)
                if(a == a[i-1]) continue;
                printf("%d\t\n",a);
        }
    system("pause");
}
我是先排序,再比較

[ 本文章最後由 phile22114 於 2010-10-8 13:41 編輯 ]

JSD-Google [/img] :.| ..ゑゑ [/url]
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 好懷念的東西 .. 想當初也接觸了一年

JSD-Google [/img] :.| ..╮╮ [/url]

高雄在地人的燈光藝術Show - 點上圖
邪惡分享 - 富士pivi MP-300
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 搞錯了@@"
這是要從文件中讀取並由文件顯示(ex:記事本)

JSD-Google [/img] :.| ..ии [/url]
原 來 " 平 淡 " 也 可 以 這 麼 美 好 ! !
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 搞錯了@@"
這是要從文件中讀取並由文件顯示(ex:記事本) [/quote]
執行完寫入記事本?
還是只要讀入馬上就寫出阿?
差蠻多的...

由小老婆網管Jorsindo...... .. [/url]
本文章最後由 site-admin 於 2015-12-27 17:52 編輯 英文題目看一下,是說從一個檔案讀取20個數值,當數值已存在時,就略過不印出。
國外的題目比較有深度,不知道LZ是不是在國外讀書。
我之前幫人寫過JAVA,在國外讀書的人,我看到題目都比台灣的難,不過對於會寫程式的人是還好。
已題目來看是一個文字檔,第一行要放10個值,第二行要放時10個值,並以Space做分割讀取十個值。
他問的是C++怎麼當作C來寫,這是比較無法認可的事情C就是C,C++就是C++。
C++開頭是include這個iostream,還要使用std命名空間。
[ 本文章最後由 hollowaysxp 於 2010-10-15 11:46 編輯 ]

JSD-Google [/img] りり.りり. .. [/url]