基礎(chǔ)練習(xí) 01字串 時(shí)間限制:1.0s 內(nèi)存限制:256.0MB 問題描述對(duì)于長(zhǎng)度為5位的一個(gè)01串,每一位都可能是0或1,一共有32種可能。它們的前幾個(gè)是:
00000
00001
00010
00011
00100
請(qǐng)按從小到大的順序輸出這32種01串。
輸入格式本試題沒有輸入。輸出格式輸出32行,按從小到大的順序每行一個(gè)長(zhǎng)度為5的01串。樣例輸出00000000010001000011<以下部分省略>#include <iostream>using namespace std;int main(){ int a[5] = {0}; for(int i = 0;i < 32;i++) { int n = i, j = 0; while(n != 0) { //思想就是將每一個(gè)數(shù)轉(zhuǎn)換為二進(jìn)制,存進(jìn)數(shù)組,然后倒序輸出 a[j++] = n % 2; n /= 2; } for(int k = 4; k >= 0; k--) { cout<<a[k]; } cout<<endl; } return 0;}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注