基础练习 01字串
时间限制:1.0s 内存限制:256.0MB
提交此题 锦囊1 锦囊2
问题描述
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
输入格式
本试题没有输入。
输出格式
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
分析:最笨的方法,像秒表一样二进制循环加一。
当然,可能有很简单的方法,但是我现在还不会,任性(~ ̄▽ ̄)~
#include <iostream>
using namespace std;
int main()
{
char str[6] = "00000";
while(str[0] != '2')
{
cout << str << endl;
str[4] += 1;
if(str[4] == '2')
{
str[3] += 1;
str[4] = '0';
if(str[3] == '2')
{
str[2] += 1;
str[3] = '0';
if(str[2] == '2')
{
str[1] += 1;
str[2] = '0';
if(str[1] == '2')
{
str[0] += 1;
str[1] = '0';
}
}
}
}
}
return 0;
}