"Written test" 30, printing 1 to the maximum number of n digits

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.