01字串
题目描述
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
输入
无
输出
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
来源/分类
蓝桥杯
解
打表或者二级制操作
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
int main()
{
for (int i = 0; i <= 31; i++){
for (int j = 4; j >= 0; j--)
cout << ((i >> j)&1);
cout << endl;
}
return 0;
}
或者
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
int main()
{
printf("00000\n00001\n00010\n00011\n00100\n00101\n00110\n00111\n01000\n01001\n01010\n01011\n01100\n");
printf("01101\n01110\n01111\n10000\n10001\n10010\n10011\n10100\n10101\n10110\n10111\n11000\n11001\n11010\n11011\n11100\n11101\n11110\n11111\n");
return 0;
}