• 蓝桥杯 基础练习 01字串(水题)


    基础练习 01字串  

    时间限制:1.0s   内存限制:256.0MB
    问题描述

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

    00000

    00001

    00010

    00011

    00100

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

    输入格式
    本试题没有输入。
    输出格式
    输出32行,按从小到大的顺序每行一个长度为5的01串。
    样例输出
    00000
    00001
    00010
    00011
    <以下部分省略>
      按从小到大的顺序输出0到31的二进制数。
      一开始把所有情况都写上输出,后来找出每一位位数变化的规律然后写个循环就做做出来了。
    暴力:
     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     cout<<"00000"<<endl;
     6     cout<<"00001"<<endl;
     7     cout<<"00010"<<endl;
     8     cout<<"00011"<<endl;
     9     cout<<"00100"<<endl;
    10     cout<<"00101"<<endl;
    11     cout<<"00110"<<endl;
    12     cout<<"00111"<<endl;
    13     cout<<"01000"<<endl;
    14     cout<<"01001"<<endl;
    15     cout<<"01010"<<endl;
    16     cout<<"01011"<<endl;
    17     cout<<"01100"<<endl;
    18     cout<<"01101"<<endl;
    19     cout<<"01110"<<endl;
    20     cout<<"01111"<<endl;
    21     cout<<"10000"<<endl;
    22     cout<<"10001"<<endl;
    23     cout<<"10010"<<endl;
    24     cout<<"10011"<<endl;
    25     cout<<"10100"<<endl;
    26     cout<<"10101"<<endl;
    27     cout<<"10110"<<endl;
    28     cout<<"10111"<<endl;
    29     cout<<"11000"<<endl;
    30     cout<<"11001"<<endl;
    31     cout<<"11010"<<endl;
    32     cout<<"11011"<<endl;
    33     cout<<"11100"<<endl;
    34     cout<<"11101"<<endl;
    35     cout<<"11110"<<endl;
    36     cout<<"11111"<<endl;
    37     return 0;
    38 }

    循环:

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

     Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    spring事务注解@Transactional注解失效场景
    Dubbo中服务消费者和服务提供者之间的请求和响应过程
    说说Java的Unsafe类
    java程序二叉树的深度优先和广度优先遍历
    重复注解与类型注解
    git pull 和 git fetch的区别?
    Java8新特性系列(Interface)
    二十种健康食品排行榜
    赞美的时机
    越过胆怯这道栅栏
  • 原文地址:https://www.cnblogs.com/yym2013/p/3506681.html
Copyright © 2020-2023  润新知