訓練1:刪除序列中相同的數

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   2014   

題目要求:

  有16個數{1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10},已經按由大到小順序排好,儲存在數組a中

  試建立一個類ARR,完成刪除數組中相同的數,經過刪除後,數組中的內容為{1,2,3,4,5,6,7,8,9,10}.

#include<iostream>#include<iomanip>using namespace std;class ARR{        public:                void ARR_delete();                ARR();//建構函式的聲明不能用void                void ARR_show();        private:                int a[16];};ARR::ARR()//不能是void ARR::ARR(){        int i;        for(i = 0;i<16;i++)        {                cin>> a[i];        }}void ARR::ARR_delete(){        int i,j,temp;        temp = a[0];        for(j = 0;j<15;j++)        {                temp = a[j];                for(i = j+1 ;i<16;i++)                {                        if(temp == a[i])                        {                                a[i]= 0;//這種消除法是比較損耗記憶體的                        }                }        }}void ARR::ARR_show(){        int i,j;        for(i= 0;i<16;i++)        {                if(a[i] !=0)                        cout <<setw(6)<< a[i];        }}int main(){        ARR ARR_test;//定義一個對象;        ARR_test.ARR_delete();        ARR_test.ARR_show();        return 0;}                                                                                                                                     

 

 

總結編寫的時候出現的bug

15行出現了傳回值什麼:錯誤是因為聲明建構函式是void ARR();其實只是ARR就可一了

40行那個也是一樣,void ARR::ARR(),其實只需要ARR::ARR();

 

函數改進的方法:

int main(){        /*ARR ARR_test;//定義一個對象;        ARR_test.ARR_delete();        ARR_test.ARR_show();*/        ARR *pt = new ARR;        pt->ARR_delete();        pt->ARR_show();        delete pt;                return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.