01字串

问题描述

对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:

00000

00001

00010

00011

00100

请按从小到大的顺序输出这32种01串。

分析:1.观察得出第i-1行即为i-1所对应的二进制数,所以就转化成了十进制数转化为二进制数

           2.初始化数组存在区别

           3.没有必要写成二维数组,每行写一个一维数组就可,最后要逆序输出

源代码:

#include<iostream>
using namespace std;
int main()
{
    int m,i,n,j;
    for(i=0;i<=31;i++)
    {
       int shu[5]={0};
//        int shu[5];
//        shu[5]={0};//无效初始化
        m=i;
        n=0;
        while(m!=0)
        {
            shu[n]=m%2;
            n++;
            m/=2;
        }
        for(j=4;j>=0;j--)
            cout<<shu[j];
        cout<<endl;
    }
    return 0;
}


posted @ 2020-03-02 09:17  Joelin12  阅读(194)  评论(0)    收藏  举报