『蓝桥杯』basic 思路: 参考: 代码:

『蓝桥杯』 BASIC-2_01字串

问题描述

对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:

00000

00001

00010

00011

00100

请按从小到大的顺序输出这32种01串。

输入格式

本试题没有输入。

输出格式

输出32行,按从小到大的顺序每行一个长度为5的01串。

样例输出

00000
00001
00010
00011
<以下部分省略>

思路:

1、暴力输出 +

2、暴力枚举 +

3、逢二进一 -

4、十 —> 二 +

参考:

https://blog.csdn.net/u012110719/article/details/41870877

代码:

2、暴力枚举 +

#include <iostream>
using namespace std;
int main(){
  int a,b,c,d,e;
  for(a=0;a<2;++a)
    for(b=0;b<2;++b)
        for(c=0;c<2;++c)
            for(d=0;d<2;++d)
                for(e=0;e<2;++e)
                    cout<<a<<b<<c<<d<<e<<endl;
  return 0;
}

4、十 -> 二 +

#include<iostream>
using namespace std;
int main(){
    for(int i=0; i<32; ++i){
        cout << i%32/16 << i%16/8 << i%8/4 << i%4/2 << i%2 <<endl;
    }
    return 0;
} 


This blog is under a
CC BY-NC-SA 3.0 Unported License


本文链接:https://idforhyit.github.io/2019/02/12/BASIC-2/