zoukankan      html  css  js  c++  java
  • 打印1到最大的n位数

    package testcase;
    
    /**
     * 
     * @decription 打印1到最大的n位数
     * @author bjliuzezhou
     * @date 2016年2月24日
     */
    public class TypicalArithmetic_04 {
    
        private static int newLine = 0;
        
        public static void main(String[] args) {
            
            int n=3;
            String s = Increment(getInputString(n));
            PrintNumber(s);
            s = Increment(s);
             
            //判断是否达到最大n位数 
            //如果字符串再次返回类似"000",则说明已经达到了最大n位数
            while(!s.equals(getInputString(n))){
                
                 PrintNumber(s);
                 s = Increment(s);
            }
            
        }
    
        //根据要打印的最大n位数  确定输入"00"(n=2) 还是"000"(n=3)
        public static String getInputString(int n){
            String s = "";
            for(int i=0; i<n; i++){
                s+="0";
            }
            return s;
        }
        
        
        //模拟字符串自增
        public static String Increment(String s){
            
            char ch[] = new char[s.length()];
            int sum = 0;
            int Len = s.length();
            for(int i=Len-1; i>=0; i--){
                ch[i] = s.charAt(i);
            }
            
            for(int i=Len-1; i>=0; i--){
                
                sum = ch[i] - '0' + 1;
                if(sum==10){
                    ch[i] = '0';
                }else{
                    ch[i] = (char)(sum+'0');
                    break;
                }
            }
            
            String tmp="";
            for(int i=0; i<Len; i++){
                tmp+=ch[i];
            }
            return tmp;
        }
        
        
        //打印字符串   
        public static void PrintNumber(String s){
            
            boolean firstLetter = true;
            
            for (int i = 0; i < s.length(); i++) {
                char ch = s.charAt(i);
                //如012不能打印前面的0   只打印12
                if (firstLetter == true) {
                    if (ch != '0') {
                        System.out.print(ch);
                        firstLetter = false;
                    }
                } else {
                    System.out.print(ch);
                }
                
            }
            
            //调整输出格式   变得好看一点
            System.out.print(" ");
            newLine++;
            if(newLine==100){
                System.out.println("
    ");
                newLine = 0;
            }
        }
    }
  • 相关阅读:
    【go语言学习】标准库之time
    【go语言学习】文件操作file
    【go语言学习】反射reflect
    【go语言学习】通道channel
    soap添加
    ubuntu apache 启用gzip
    git 版本回退
    ubuntu打开crontab日志及不执行常见原因
    Ionic3 怎么打开第三方 app,最简单粗暴的方法
    Windows安装使用Openssl创建pks p12证书
  • 原文地址:https://www.cnblogs.com/cangqiongbingchen/p/5234200.html
Copyright © 2011-2022 走看看