編程珠璣–第一章使用位元影像

來源:互聯網
上載者:User

問題描述:一個最多包括n個正整數的檔案,每個數都小於n,其中n=10000000.如果在輸入檔案中有任何整數重複出現的話就是致命錯誤。要求按升序輸出這些數。

由於我們只能操縱位元組,但是我們可以使用位元組來操縱位元組中的位,我們使用int(4個位元組),如果我們想要將第k位置為1的話,我們只需要這樣操作:

a[k/32]|pow(2,31-k%32),當我們要判斷某位是不是1的時候,使用相似的方法:a[k/32]&pow(2,31-k%32)

#include<iostream>#include<cmath>using namespace std;int a[312500];int main(){memset(a,0,sizeof(a));//讀檔案int readin;while(cin>>readin)a[readin/32]=a[readin/32]|(int)pow(2.0,31-readin%32);//判斷for(int i=0;i<10000000;i++)if((a[i/32]&(int)pow(2.0,31-i%32))!=0)cout<<i<<endl;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.