Input number N, output the n-digit 10th hexadecimal number from 1 in order

Source: Internet
Author: User

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;}}}}}}


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.