文章

54

粉丝

66

获赞

54

访问

3.5k

头像
01序列 (二进制枚举)题解:
P1001 计算机考研机试入门题
发布于2024年4月21日 17:24
阅读数 118

 经过分析,只需要枚举0到64的二进制数即可

#include<bits/stdc++.h>
using namespace std;

int res; //全局变量系统初始化为0

int main()
{
	printf("%06d\n", res); //先输出六个0
	for(int i = 1; i < 64; i ++) //枚举
	{
		string ans;
		int n = i;
		while(n) //转换成二进制
		{
			ans.push_back((n & 1) + '0');
			n >>= 1;
		}
		
		reverse(ans.begin(), ans.end());  //由于是倒着存的翻转一下
	
		res = stoi(ans); //转换成数字格式化输出
	
		printf("%06d\n", res);
	
	}
	
	return 0;	
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发