01字符串——算法

01字符串

问题描述

问题描述

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

00000

00001

00010

00011

00100

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

输入格式

本试题没有输入。

输出格式

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

样例输出

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

问题分析

就是将0-32以五位二进制数的形式输出,可以试着采用拼接形式

代码实现

#include <iostream>
#include <bits/stdc++.h>
using namespace std;  
int main(){
 for(int a = 0; a < 2; a++) {
		   for(int b = 0; b < 2; b++) {
			   for(int c = 0; c < 2; c++) {
				   for(int d = 0; d < 2; d++) {
					   for(int e = 0; e < 2; e++) {
						   cout<<a<<b<<c<<d<<e<<endl;
					   }
				   }
			   }
		   }
	   }
	return 0;
}

运行结果

00000
00001
00010
00011
00100
00101
00110
00111
01000
01001
01010
01011
01100
01101
01110
01111
10000
10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11101
11110
11111

--------------------------------
Process exited after 2.333 seconds with return value 0
请按任意键继续. . .

总结

采用五重循环,最外面一层循环枚举最左边一位,第二层循环枚举左边第二位,依次类推,第五层循环枚举最低位。

发布了60 篇原创文章 · 获赞 5 · 访问量 5071

猜你喜欢

转载自blog.csdn.net/qq_38496329/article/details/104056418