Calculate the n-digit decimal number ranging from 1 to the maximum-a large number.
Requirements: Enter a number n and print the n from 1 to the maximum in decimal order. For example, 3 is input, and 1, 2, 3... is printed ...... Up to the maximum 3-digit 999
It seems very simple. Barabara has obtained the following code:
/*** Note: when the value of n is large, the maximum number of digits * @ param n */public static void Print1ToMaxOfNDigits_1 (int n) will overflow) {int number = 1; int I = 0; while (I ++ <n) {number * = 10;} for (I = 0; I <number; I ++) {System. out. println (I + "\ t ");}}
Well, okay. It's a perfect mistake. At first glance, there seems to be no problem, but after careful analysis, it seems that this n does not provide a range. If the value of this n is particularly large, can int be accommodated? Well, we can use the long type, but if n is bigger, will it overflow! Okay,What we encounter now is the big number problem.. How can we still get the expected results when n is large? We can simulate using strings or arrays. The following code simulates a character array:
/*** Simulate numbers using arrays to solve the problem of large number overflow * @ param n maximum number of digits */public static void Print1ToMaxOfNDigits (int n) {if (n <0) {return;} char [] number = new char [n]; // assign the initial value 0 for the number array (int I = 0; I <n; I ++) {number [I] = '0';} while (! Increment (number) {PrintNumber (number );}}
Character array value plus 1 Function
/*** Implement the function of adding 1 to the number array * @ param number numeric array * @ return indicates whether the overflow exists, that is, whether the maximum number of all 1-n bits has been printed, true */public static Boolean Increment (char [] number) {Boolean isOverflow = false; int nTakeOver = 0; int nLength = number. length; for (int I = nLength-1; I> = 0; I --) {int nSum = number [I]-'0' + nTakeOver; if (I = nLength-1) // The percentile of the number {nSum ++;} if (10 <= nSum) // generates carry {if (0 = I) // generate the carry value is the highest bit, which proves that all numbers have been printed {isOverflow = true;} else // generate the carry value not the highest bit {nSum-= 10; nTakeOver = 1; // Add 1 number [I] = (char) ('0' + nSum);} else {number [I] = (char) ('0' + nSum); break;} return isOverflow ;}
Obviously, when we print the array, if we do not perform any operation, if there is a number less than n, there will be 0 in the front, and the output is a little different from what we encounter in our daily life. It feels awkward, therefore, the output format of the character array is rewritten here, and the unnecessary 0 is removed.
/*** Print out the array and remove the array to be printed by 0 * @ param number before the array */public static void PrintNumber (char [] number) {Boolean isBeginning0 = true; int nLength = number. length; for (int I = 0; I <nLength; I ++) {if (isBeginning0 & '0 '! = Number [I]) // used to determine whether the start of {isBeginning0 = false;} if (! IsBeginning0) {System. out. print (number [I]) ;}} System. out. println ("");}
Test
public static void main(String[] args) { //Print1ToMaxOfNDigits_1(25); Print1ToMaxOfNDigits(2); }
Summary:
When dealing with related numbers, we need to consider whether there will be a particularly large amount of data. If so, we can replace it with arrays or strings.