蓝桥杯 基础练习 01字串
http://lx.lanqiao.cn/problem.page?gpid=T6
问题描述
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
输入格式
本试题没有输入。
输出格式
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
00001
00010
00011
<以下部分省略>
代码:
#include <bits/stdc++.h>
using namespace std;
string BinarySystem(int x) {
string s = "";
while(x) {
s += x % 2 + '0';
x /= 2;
}
while(s.length() != 5)
s += '0';
int len = s.length();
for(int i = 0; i <= len / 2; i ++)
swap(s[i], s[len - 1 - i]);
return s;
}
int main() {
for(int i = 0; i <= 31; i ++)
cout << BinarySystem(i) << endl;
return 0;
}

浙公网安备 33010602011771号