輸入一個數字n,列印1到n位數的最大值。eg:n=3,代表列印1-999之間的數字
阿新 • • 發佈:2018-12-15
相關解決:
package com.swordRefersToOffer; import java.util.Arrays; /** * @author 凌龍 * @date 2018/9/28 */ public class Test { public static void main(String[] args) { Test test = new Test(); // test.printToMax(1); test.printToMax2(2); } /** *方法一: */ /**字串上模擬加法 * * @param n */ public void printToMax(int n){ if(n < 0) return; char[] number = new char[n]; // 初始化 // for(int i = 0; i < n; i++) number[i] = '0'; Arrays.fill(number, '0');//字串填充0; while(!increment(number)){ printNumber(number); } return; } public boolean increment(char[] num){ boolean isOverflow = false; int size = num.length; int carry = 0; for(int i = size - 1; i >= 0; i--){ int temp = num[i] - '0' + carry; if(i == size - 1) temp++; if(temp >= 10){ if(i == 0) //最高位溢位 isOverflow = true; else{ temp -= 10; carry = 1; num[i] = (char) ('0' + temp); } }else{ num[i] = (char)('0' + temp); break; } } return isOverflow; } public void printNumber(char[] num){ int size = num.length; int i = 0; while(i < size && num[i] == '0') //i < size在前,否則越界 i++; //char[] printNum = new char[size - i]; //System.arraycopy(num, i, printNum, 0, size - i);//複製陣列 if(i == size)//不列印全0 return; char[] printNum = Arrays.copyOfRange(num, i, size);//複製陣列 System.out.println(printNum); } /** *方法二: */ /**字元每一位進行全排列 * * @param n */ public void printToMax2(int n){ if(n <= 0) return; char[] number = new char[n]; Arrays.fill(number, '0'); printOrder(number,n,0); } public void printOrder(char[] number, int n, int loc){ if(loc == n) return; for(int i = 0; i <= 9; i++){ number[loc] = (char)('0' + i); if(loc == n - 1){ printNumber(number); } printOrder(number,n,loc + 1); } } }