zoukankan      html  css  js  c++  java
  • 剑指Offer:面试题12——打印1到最大的n位数(java实现)

    问题描述:

    输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的3位数即999.

    思路1:最简单的想法就是先找出最大的n位数,然后循环打印即可。

    public static void Print1ToMaxOfNDigits_1(int n){
        int number = 1;
        int i = 0;
        while(i++ < n){
            number *= 10;
        }
        //number-1是最大的n位数
        for(int j = 1; j < number; j++){
            System.out.println(j);
        }
    }

    然而上述程序在n很大时,显然会有溢出,int 的范围不够。那么我们会想到用long来存储,但是如果n的值还是很大,以至于long也无法满足要求。那么该怎么办?

    只能用其他的数据结构来存储我们的非常大的数字。

    思路2:用字符串来存储数字。

        public static void Print1ToMaxOfNDigits_2(int n){
    
            if(n <= 0){
                return;
            }
            StringBuffer number = new StringBuffer();
    
            for(int i = 0; i < n; i++){
                number.append('0');
    
            }
    
            while(!Increment(number)){
                PrintNumber(number);
            }
        }
        public static boolean Increment(StringBuffer s){
            boolean isOverflow = false;
            int nTakeOver = 0;
            int nLength = s.length();
            for(int i = nLength - 1; i >= 0; i--){
                int nSum = s.charAt(i) - '0' + nTakeOver;
                if( i == nLength - 1){
                    nSum++;
                }
                if(nSum >= 10){
                    if(i == 0){
                        isOverflow = true;
    
                    }else{
                        nSum -= 10;
                        nTakeOver = 1;
                        s.setCharAt(i, (char) ('0' + nSum));
                    }
    
                }else{
                    s.setCharAt(i, (char) ('0' + nSum));
                    break;
                }
            }
            return isOverflow;
        }
    
        public static void PrintNumber(StringBuffer s){
            boolean isBeginning0 = true;
            for(int i = 0; i < s.length(); i++){
                if(isBeginning0 && s.charAt(i) != '0'){
                    isBeginning0 = false;
                }
                if(!isBeginning0){
                    System.out.print(s.charAt(i));
                }
            }
    
            System.out.println();
        }

    Increment要注意终止条件
    打印函数:要注意处理前面的‘0’字符

    思路3:用数字排列的方法表示:如果我们在数字前面补0的话,就会发现n位所有十进制数其实就是n个从0到9的全排列。也就是说,我们把数字的每一位都从0到9排列一遍,就得到了所有的十进制数。当然打印的时候,我们应该将前面的0补位去掉。

    public static void Print1ToMaxOfNDigits_3(int n){
            if(n < 0){
                return;
            }
            StringBuffer s = new StringBuffer(n);
            for(int i = 0; i < n; i++){
                s.append('0');
            }
            for(int i = 0; i < 10; i++){
    
                s.setCharAt(0, (char) (i+'0'));
                Print1ToMaxOfNDigits_3_Recursely(s, n, 0);
            }
    
        }
        public static void Print1ToMaxOfNDigits_3_Recursely(StringBuffer s, int n , int index){
            if(index == n - 1){
                PrintNumber(s);
                return;
            }
    
            for(int i = 0; i < 10; i++){
                s.setCharAt(index+1, (char) (i+'0'));
                Print1ToMaxOfNDigits_3_Recursely(s, n, index+1);
            }
        }
     public static void PrintNumber(StringBuffer s){
            boolean isBeginning0 = true;
            for(int i = 0; i < s.length(); i++){
                if(isBeginning0 && s.charAt(i) != '0'){
                    isBeginning0 = false;
                }
                if(!isBeginning0){
                    System.out.print(s.charAt(i));
                }
            }
    
            System.out.println();
        }
  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/wenbaoli/p/5655724.html
Copyright © 2011-2022 走看看