您的位置:首页 > 其它

2013校园招聘---暴风影音---笔试题--输入n,输出对应的所有长度为n的二进制串

2012-10-25 13:27 393 查看
输入1

输出:

0

1



输入3:

输出:

000

001

010

011

100

101

110

111



//暴风影音 2013 校园招聘 笔试题
#include <iostream>
#include <math.h>
#include <stack>
using namespace std;

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

void main()
{
	print(3);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐