打印小於n位的所有數字

package com.dugstudio.SwordToOfferBook.Singleton.Interview;

/**
 * @Author JH
 * @CreateDate 18-6-8
 * @Descriptionv 從1 打印n位數 eg:1,2,3...,999...999
 */
public class Print1ToMaxOfNDigits {
    public void  Print1ToMaxOfNDigits(int n) {
        byte number[] = new byte[n];
        for (byte i = 0; i < 10; i++) {
            number[0]=i;
            Print1ToMaxOfNDigitsByRecrusive(number, n, 0);
        }
    }
    public void Print1ToMaxOfNDigitsByRecrusive(byte [] number ,int len, int index){
        if (index==len-1){
            printNumber(number);
            return ;
        }
        for (byte i=0;i<10;i++){
            number[index+1]=i;
            Print1ToMaxOfNDigitsByRecrusive(number,len,index+1);
        }
    }
    //從左第一位非0位至右打印
    public void printNumber(byte[] number){
          boolean flag=false;
        for (byte b:number
             ) {
            if (b!=0)flag=true;
            if (flag){
                System.out.print(b);
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Print1ToMaxOfNDigits p=new Print1ToMaxOfNDigits();
        p.Print1ToMaxOfNDigits(3);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章