蓝桥杯之01字符串
程序员文章站
2022-07-03 21:03:13
...
方法一:
#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;
}
方法二:
int main()
{
for(int a=0;a<=1;a++)
{
for(int b=0;b<=1;b++)
{
for(int c=0;c<=1;c++)
{
for(int d=0;d<=1;d++)
{
for(int e=0;e<=1;e++)
cout<<a<<b<<c<<d<<e<<endl;
}
}
}
}
return 0;
}
方法三:
#include<bits/stdc++.h>
#define PI atan(1.0)*4
using namespace std;
/*char a[16][5]={"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"};*/
int main()
{
string s="00000";
for(int i=0;i<32;i++)
{
cout<<s<<endl;
s[4]+=1;
for(int j=4;j>=0;j--)
{
if(s[j]=='2')
{
s[j-1]+=1;
s[j]='0';
}
}
}
return 0;
}
关于string的一点解释:
int main()
{
string s="00000";
s[4]+=1;//实际上是ASCII码相加,‘0’对应的ASCII码是48,执行加1操作后变为49
cout<<s<<endl;//输出结果00001,s[4]是ASCII码为49对应的字符
return 0;
}
int main()
{
string s="00000";
s[4]+='1';//’0‘的ASCII码为48,’1‘的ASCII码为49,相加后为97
cout<<s<<endl;//输出结果为000a,s[4]是ASCII码为97对应的字符
return 0;
}
附上string的用法:(https://blog.csdn.net/qq_37941471/article/details/82107077)