「这是我参与2022首次更文挑战的第16天,活动详情查看:2022首次更文挑战」
💛作者主页:静Yu
🧡简介:CSDN全栈优质创作者、华为云享专家、前端知识交流社区创建者
💛社区地址:bbs.csdn.net/forums/Jing…
第十三届蓝桥杯大赛个人赛省赛比赛将于2022年4月9日(星期六)举办,趁现在寒假的时间抓紧时间备战一下。因为博主本人报名是C/C++组,所以更新所有内容都是C/C++相关知识。题目全部都是蓝桥杯官网题库真题。今天是备战刷题的第十天。
题目:
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
输出格式:
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出:
00000
00001
00010
00011
<以下部分省略>
解题思路:
本道题目就是我们平时算的二进制加法,逢二进一。最简答的解题方式就是暴力解题法,将所有的情况列举出来。题目已经写出一共有32种情况,简单列出即可。
第二种解题的的方式就是模拟二进制运算,主要思想是逢二进一,一共是有32种情况,循环输出,一个数有五位,一位一位进行判断,如果这一位的数等于二的话,字符串的前一位加1,后一位变为0。
完整代码:
//暴力方式
#include <iostream>
using namespace std;
int main()
{
cout<<"00000"<<endl;
cout<<"00001"<<endl;
cout<<"00010"<<endl;
cout<<"00011"<<endl;
cout<<"00100"<<endl;
cout<<"00101"<<endl;
cout<<"00110"<<endl;
cout<<"00111"<<endl;
cout<<"01000"<<endl;
cout<<"01001"<<endl;
cout<<"01010"<<endl;
cout<<"01011"<<endl;
cout<<"01100"<<endl;
cout<<"01101"<<endl;
cout<<"01110"<<endl;
cout<<"01111"<<endl;
cout<<"10000"<<endl;
cout<<"10001"<<endl;
cout<<"10010"<<endl;
cout<<"10011"<<endl;
cout<<"10100"<<endl;
cout<<"10101"<<endl;
cout<<"10110"<<endl;
cout<<"10111"<<endl;
cout<<"11000"<<endl;
cout<<"11001"<<endl;
cout<<"11010"<<endl;
cout<<"11011"<<endl;
cout<<"11100"<<endl;
cout<<"11101"<<endl;
cout<<"11110"<<endl;
cout<<"11111"<<endl;
return 0;
}
//模拟二进制运算
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i,j;
string str="00000";
for(i=0;i<32;++i)
{
cout<<str<<endl;
str[4]+=1;
for(j=4;j>=0;--j) //一个数有五位
{
if(str[j]=='2') //逢二进一
{
str[j-1]+=1; //前一位加一
str[j]='0'; //原来位置变为0
}
}
}
return 0;
}