> 文章列表 > 蓝桥杯基础13:BASIC-2试题 01字串

蓝桥杯基础13:BASIC-2试题 01字串

蓝桥杯基础13:BASIC-2试题 01字串

资源限制

内存限制:256.0MB   C/C++时间限制:1.0s   Java时间限制:3.0s   Python时间限制:5.0s

问题描述

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

00000

00001

00010

00011

00100

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

输入格式

本试题没有输入。

输出格式

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

样例输出

00000
00001
00010
00011
<以下部分省略>

法一:

#include<stdio.h>
int main(){int a,b,c,d,e;for(a=0;a<=1;a++)for(b=0;b<=1;b++)for(c=0;c<=1;c++)for(d=0;d<=1;d++)for(e=0;e<=1;e++)printf("%d%d%d%d%d\\n",a,b,c,d,e);return 0;
}

法二: 

#include <stdio.h>
int main()
{int i;for (i = 0; i < 32; i++)printf("%d%d%d%d%d\\n", i/16%2, i/8%2, i/4%2, i/2%2, i%2);return 0;
}