Tag:java sword refers to offer written test
/** * Title: Enter the number n, in order to print from 1 the largest n-bit decimal number, such as input 3, then print out 1, 2, 31 until the largest 3-digit number is 999. * Time: August 29, 2015 09:32:48 * file: Maxofndigits_1.java *cutter_point */package Bishi. Offer50.y2015.m08.d29;import org.junit.*;p ublic class maxofndigits_1{/** * Print out these numbers * @param n */public void Print1tomaxof Ndigits (int n) {if (n <= 0) return;//from 1 to n digits, which is to create an array of length n char number[] = new Char[n];for (int i = 0; i < n; ++i) {//Initialize to 0 Number[i] = ' 0 ';} Forint i = 0;//as long as there is no overflow, we continuously output while (!increment (number)) {++i; System.out.print (string.valueof (number) + "\ T"), if (i% = = 0) System.out.println ();}} This topic involves the large number of private Boolean increment (char number[]) {Boolean overflow = false;//to determine if an overflow int nTake10 = 0;//carry int num = 0;for (in t i = number.length-1; I >= 0; -i) {num = number[i]-' 0 ' + ntake10;if (i = = number.length-1) ++num;if (num >= 10) {//carry if (i = = 0) {overflow = true;} If overflow else{ntake10 = num/10;num = num% 10;number[i] = (char) (num + ' 0 ');} else}//ifelse{//no rounding, then end number[i] = (char) (num + ' 0 '); break;} Else}//forreturn overflow;} @TestpublIC void Test () {maxofndigits_1 mond1 = new Maxofndigits_1 (); mond1.print1tomaxofndigits (10);} public static void Main (string[] args) {}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Written test" 30, printing 1 to the maximum number of n digits