蓝桥杯练习题 之 01字串

基础练习 01字串  

时间限制:1.0s   内存限制:256.0MB

      

问题描述

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

00000

00001

00010

00011

00100

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

输入格式

本试题没有输入。

输出格式

输出32行,按从小到大的顺序每行一个长度为5的01串。

样例输出

00000
00001
00010
00011

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
   int i,j,t;
    for(i=0;i<32;i++)
       {
           int a[5]={0};
           j=4;
           t=i;  //保存此时的i
           while(t>=1)
           {
               a[j--]=t%2;//逆序存入
               t/=2;
           }
           for(j=0;j<5;j++)
            cout<<a[j];
           cout<<endl;
       }

}

发布了24 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/xindada559/article/details/84898435