• 01字串_蓝桥杯


    类似贪心算法

    /**
    问题描述
    对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
        
        00000
        
        00001
        
        00010
        
        00011
        
        00100
    
    请按从小到大的顺序输出这32种01串。
    
    输入格式
        本试题没有输入。
    输出格式
        输出32行,按从小到大的顺序每行一个长度为5的01串。
    样例输出
        00000
        00001
        00010
        00011
    <以下部分省略>
     */
    package jiChuLianXi;
    
    public class String01 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            StringBuilder str = new StringBuilder();
            for(int i=0; i<32; i++){
                int n = i;
                if(n>=16){
                    System.out.print("1");
                    n -= 16;
                }else{
                    System.out.print("0");
                }
                if(n>=8){
                    System.out.print("1");
                    n -= 8;
                }else{
                    System.out.print("0");
                }
                if(n>=4){
                    System.out.print("1");
                    n -= 4;
                }else{
                    System.out.print("0");
                }
                if(n>=2){
                    System.out.print("1");
                    n -= 2;
                }else{
                    System.out.print("0");
                }
                if(n>=1){
                    System.out.println("1");
                    n -= 1;
                }else{
                    System.out.println("0");
                }
            }
        }
    
    }
  • 相关阅读:
    之前的博客
    用struts2 s2-045漏洞拿站记录
    修改BlackLowKey皮肤样式,增加占屏比
    SpringBoot自动配置原理
    CAS无锁技术
    CAS单点登录原理解析
    死锁与活锁的区别,死锁与饥饿的区别
    jvm问题
    jdk动态代理的实现原理
    抽象工厂
  • 原文地址:https://www.cnblogs.com/LieYanAnYing/p/12190197.html
Copyright © 2020-2023  润新知