输入n,输出对应的所有长度为n的二进制串

#include <iostream>
#include <stack>
#include <math.h>
using namespace std;

void print(int n)
{
	int max = (int)pow((double)2,(double)n);
	unsigned int mask = 0x00000001;
	int i,j;
	stack<int> stack1;
	for(i=0;i<max;i++)
	{
		for(j=0;j<n;j++)
		{
			stack1.push(i&mask?1:0);
			mask<<=1;
		}
		while(stack1.size())
		{
			cout<<stack1.top();
			stack1.pop();
		}
		cout<<endl;
		mask>>=n;
	}
}

int main()
{
	cout<<"请输入长度n:";
	int n;
	cin>>n;
	cout<<"所有长度为n的二进制串为:"<<endl;
	print(n);
	return 0;
}
输入n,输出对应的所有长度为n的二进制串_第1张图片

你可能感兴趣的:(输入n,输出对应的所有长度为n的二进制串)