蓝桥 BASIC-2

#include<iostream>
#include<bitset> 
using namespace std;
int a[5];
int main()
{
	//方法一 模拟一个加一的过程 从最低位开始加一  若此位为1 则变0  向高位进位 
	/*for(int i=0;i<32;i++)
	{
		for(int i=0;i<5;i++)cout<<a[i];
		cout<<endl;
		for(int j=4;j>=0;j--)
		{
			if(a[j]==0)
			{
				a[j]=1;
				break;
			}
			else a[j]=0;
		}
	}*/ 
	
	
	//方法二 
	/*十进制->二进制 
	0-> 00000

	1-> 00001

	2-> 00010

	3-> 00011

	4-> 00100
	......*/
	for(int i=0;i<32;i++)
	{
		bitset<5> B(i);//c++stl中的类的构造函数  头文件为<bitset>  <5>代表长度为5    
		cout<<B.to_string()<<endl;//此函数将这个对象转为string类对象 
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/artistkeepmonkey/article/details/89031819