/** * */ package jianzhioffer; /** * @Description 输入n位数,输出0-N的所有数 * @author liutao * @data 2016年4月22日 */ public class a12_print1ToMaxOfNDigits { public static void Print1ToN(int n){ if(n<=0) return ; char[] number = new char[n]; for(int i=0;i<10;i++){ number[0] = (char)(i+'0'); print1ToNRecursively(number,n,0); } } public static void print1ToNRecursively(char[] number,int length,int index){ if(index == length-1){ printNumber(number); return; } for(int i=0;i<10;++i){ number[index+1] = (char)(i+'0'); print1ToNRecursively(number,length,index+1); } } public static void printNumber(char[]num){ boolean isBeginning = true; int len = num.length; for(int i=0;i<len;i++){ if(isBeginning&&num[i]!='0'){ isBeginning = false; } if(!isBeginning){ System.out.print(num[i]); } } if(!isBeginning) System.out.println(); } public static void main(String[] args) { Print1ToN(3); } }