问题描述
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
输入格式
本试题没有输入。
输出格式
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
#include<iostream>
#include<bitset>
#include <algorithm>
#include<math.h>
using namespace std;
string intToA(int n,int radix) //n是待转数字,radix是指定的进制
{
string ans = "";
do{
int t = n % radix;
if(t>=0 && t<=9)
ans += t+'0';
else
ans += t-10+'a';
n /= radix;
}while(n != 0); //使用do{}while()以防止输入为0的情况
reverse(ans.begin(),ans.end());
return ans;
}
int count(int n,int radix)
{
int count = 0;
while(n>=radix)
{
count++;
n /= radix;
}
return count;
}
int main()
{
for(int i = 0;i < 32;i++)
{
// if(i < 0)
// cout<<"-";
// cout<<intToA(abs(i),2)<<endl;
for(int j = 0;j<4-count(i,2);j++)//方法2,使用自己写的函数
cout<<"0";
cout<<intToA(i,2)<<endl;
// cout<<bitset<5>(i)<<endl;//方法1,使用bitset
}
return 0;
}