问题描述
对于长度为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()
{
cout<<"00000"<<endl;
cout<<"00001"<<endl;
cout<<"00010"<<endl;
cout<<"00011"<<endl;
cout<<"00100"<<endl;
cout<<"00101"<<endl;
cout<<"00110"<<endl;
cout<<"00111"<<endl;
cout<<"01000"<<endl;
cout<<"01001"<<endl;
cout<<"01010"<<endl;
cout<<"01011"<<endl;
cout<<"01100"<<endl;
cout<<"01101"<<endl;
cout<<"01110"<<endl;
cout<<"01111"<<endl;
cout<<"10000"<<endl;
cout<<"10001"<<endl;
cout<<"10010"<<endl;
cout<<"10011"<<endl;
cout<<"10100"<<endl;
cout<<"10101"<<endl;
cout<<"10110"<<endl;
cout<<"10111"<<endl;
cout<<"11000"<<endl;
cout<<"11001"<<endl;
cout<<"11010"<<endl;
cout<<"11011"<<endl;
cout<<"11100"<<endl;
cout<<"11101"<<endl;
cout<<"11110"<<endl;
cout<<"11111"<<endl;
return 0;
}
之后想到这其实不就是0~32的二进制编码吗
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i,j;
string str="00000"; //初始值
for(i=0;i<32;++i) //控制32次输出,相当于加32次1
{
cout<<str<<endl;
str[4]+=1; //不断加1
for(j=4;j>=0;--j) //模拟二进制数加法,遍历所有位数,逢2进1
{
if(str[j]=='2')
{
str[j-1]+=1;
str[j]='0';
}
}
}
return 0;
}
之后观看了蓝桥杯锦囊提示,原来我想复杂了
五层循环!!!
#include <iostream>
using namespace std;
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++)
cout<<a<<b<<c<<d<<e<<endl;
return 0;
}
最后在网上看了一段
中心思想是十进制数转二进制数的操作
#include <iostream>
using namespace std;
int main()
{
for(int i=0;i<32;i++)
{
cout<<i%32/16<<i%16/8<<i%8/4<<i%4/2<<i%2<<endl;
}
return 0;
}
来源:CSDN
作者:old sweet
链接:https://blog.csdn.net/weixin_43444687/article/details/103945822