POJ 1019 Number Sequence

Source: Internet
Author: User

Approximate test instructions:

There is a string of numbers, the law of which is

1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112 K

Enter position n, calculate what number the nth digit of this string is, note the number, not the number! For example, the 10th digit of 12345678910 is 1, not 10, 11th bit is 0, nor 10. In short, the number of multiple digits in the sequence to be split into several numbers, a number corresponding to a bit.

Problem Solving Ideas:
Analog grouping, the 1 as the 1th Group, 12 as the 2nd group, 123 as the 3rd group ... Then group I is a positive integer that holds the sequence of numbers [1,i], but the length of group I is not necessarily the I

The range of n that is known to find the nth digit is (1≤n≤2147483647), then there must be at least 31,268 groups in order for the number sequence to reach the No. 2147483647 digit.

Note: 2147483647 is exactly the positive integer maximum limit () for int, so it is sufficient for n to be defined with an int. But s[31268] has more than 2147483647 digits, so use unsigned or long to define s[]

One of the math difficulties is 2:

These two mathematical laws are calculated on paper and can be found.
(int) log10 ((double) i) +1
(i-1)/(int) Pow ((double) 10,len-pos)%10

Problem-Solving ideas reference: http://blog.csdn.net/lyy289065406/article/details/6648504

1#include <cstdio>2#include <cmath>3 //The length of the array is calculated based on the range of I given by the topic .4 #defineLEN 312695 //Nnum[i] Indicates the length of the number sequence of group I6 LongNnum[len];7 //Anum[i] Indicates the length of the number sequence of the first I group8 Long LongAnum[len];9 Ten //make a table and calculate the sequence of group I One voidInit () A { -nnum[0] = anum[0] =0; -      for(inti =1; I <= LEN; ++i) the     { -Nnum[i] = nnum[i-1] + (Long) log10 (Double) i) +1; -Anum[i] = anum[i-1] +Nnum[i]; -     } +     return ; - } + //find the group where the nth digit is located A intCheckintN) at { -     inti =1; -      while(N >Anum[i]) -++i; -     //because N>anum[i], so here i-1 is the position of the group before the group N -     return--i; in } -  to intMain () + { -     intT; the init (); *scanf"%d",&t); $      while(t--)Panax Notoginseng     { -         intn,i; thescanf"%d",&n); +         //X is the subscript of the group that precedes the group where n is . A         intx =check (n); the         //Anum[x] The number sequence length of the first X group, n-anum[x] is the position of the nth digit in the group where n is located +         intCNT = n-Anum[x]; -         intlen=0; $          for(i =1; Len < CNT; ++i) $Len + = (int) log10 (Double) i) +1; -  -printf"%d\n", (I-1) / (int) Pow ((Double)Ten, len-cnt)%Ten); the     } -     return 0;Wuyi}

POJ 1019 Number Sequence

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.