Description:
Enter the number N and print the n-digit decimal number from 1 to the maximum in order. For example, if you enter 3, the number of three digits from 1, 2, and 3 to the maximum is 999.
Analysis description:
The first thought is to first calculate the maximum N-digit number, and then print it from 1 to the maximum N-digit number in a loop.
Void print1tomaxofndigits_1 (int n) {If (n <= 0) return; int I = 0; int number = 1; while (I ++ <n) {Number * = 10;} for (I = 1; I <number; ++ I) printf ("% d \ t", I );}
Note that the above Code segment only gives n digits, but it does not indicate how big the N digits are. This is a big number problem. For example, an overflow problem occurs after N> 32. Therefore, you must consider the large number problem. Use a string to express the large number, that is, each character in the string is a character between 0 and 9, used to represent one digit in the number. Because the maximum number is N bits, a string with a length of N + 1 is required (the last one in the string is the terminator '\ 0 '). When the actual number is less than N digits, add 0 to the first half of the string.
First, initialize each number in the string to '0'. Then, add 1 to each number in the string and print it out. Therefore, we need to do two things: 1. Simulate addition on numbers expressed by strings; 2. Print the numbers expressed by strings.
Void print1tomaxofndigits_1 (int n) {If (n <= 0) return; char number [n + 1]; memset (number, '0', N ); number [N] = '\ 0'; while (! Increment (number) {/* simulate addition of numbers expressed by strings */printnumber (number);/* print the numbers expressed by strings */} return ;}
# Define false 0 # define true (! False) typedef int status; Status increment (char * Number) {status isoverflow = false; int ntakeover = 0; int nlength = strlen (number ); for (INT I = nlength-1; I> = 0; I --) {int nsum = number [I]-'0' + ntakeover; if (I = nlength-1) nsum ++; If (nsum> = 10) {if (I = 0) isoverflow = true; else {nsum-= 10; ntakeover = 1; number [I] = '0' + nsum ;}} else {number [I] = '0' + nsum; break ;}} return isoverflow ;}
In the above function, you need to know when to stop adding 1 to number, that is, when to reach the maximum N-digit number. It can be compared with the maximum number after each increment (using the strcmp function), but the complexity is O (n ). When the maximum number is reached, if you continue to add 1, the maximum bit will overflow. Therefore, it is a small trap to quickly determine whether the maximum N-digit number is reached after each increase of 1. It uses O (1) Time to determine whether the maximum N-digit number has been reached.
Void printnumber (char * Number) {status isbeginning0 = true; int nlength = strlen (number); For (INT I = 0; I <nlength; ++ I) {If (isbeginning0 & number [I]! = '0') isbeginning0 = false; If (! Isbeginning0) printf ("% C", number [I]);} printf ("\ t ");}
When outputting a number, note that for a relatively small number, such as 093, we only need to output 93 instead of 093.
Note: If the interview question is about an integer of N digits and the value range of N is not limited, or an integer of any size is input, it is likely that the question of large numbers needs to be considered. A string is a simple and effective method for representing large numbers.