Question: Enter the number N and output the n-digit 10-digit number from 1 in sequence. For example, input 3,
The output values are 1, 2, 3, and the maximum 3-digit value is 999.
Analysis: when the number of digits is small, it can be output cyclically from 1 to POW (10, n) to 1 in the Integer Range.
However, when the number of digits exceeds the Integer Range, it overflows and you need to find another path. The central idea is the carry system, for example, every ten to one.
You can define an N-Bit Array to simulate a number or an N-Bit String. The value of a certain position in the array is changed because it is followed by 10.
The specific implementation is as follows:
import java.util.Scanner;public class Main {/** * @param args */public static void main(String[] args) {Scanner cin = new Scanner(System.in);int n;//while (cin.hasNext()) {n = cin.nextInt();System.out.println(n);Print1toNDigits(n);//}}static void Print1toNDigits(int n) {int i;int[] number = new int[n];for (i = 0; i < n; i++)number[i] = 0;i = n - 1;int k = n - 1, count = 1;for (i = 0; i < n; i++)count *= 10;while (count > 0) {k = n - 1;if (number[k] < 9) {++number[k];} else {while (k >= 0) {if (number[k] == 9) {number[k] = 0;--k;} else {number[k]++;break;}}}print(number);count = count - 1;}}static void print(int num[]) {int len = num.length;for (int i = 0; i < len; i++) {if (num[i] != 0) {while (i < len) {System.out.print(num[i]);++i;if (i >= len) {System.out.println();return;}}}}}}