Number Sequence
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 35823 |
|
Accepted: 10340 |
Description
A single Positive An integer I is given. Write a program to find the digit located in the position I in the sequence of number groups s1s2 ... Sk. Each group Sk consists of a sequence of positive integers numbers ranging from 1 to K, written one after another.
For example, the first digits of the sequence is as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
The first line of the input file contains a single integer t (1≤t≤10), the number of test cases, followed by one line For each test case. The line for a test case contains the single integer I (1≤i≤2147483647)
Output
There should is one output line per test case containing the digit located in the position I.
Sample Input
283
Sample Output
22
To find out the value of the first position
Idea: You can divide the sequence into several groups such as 1 123 1234 ... . Using s[]a[] Two arrays to record the length of each sequence, and the current length of each set of sequences, and then roughly find I in which group, and then in each
This value is found in the corresponding position in the small group.
#include <iostream> #include <cstdio> #include < string.h> #include <string> #include <cmath> #include <queue> #define LL long longusing namespace std ; LL s[100000],a[100000]; ll SO (ll X,ll D) {while (d--) {x/=10; } return x%10;} int main () {LL n,m,i,j,cla; A[1]=s[1]=1; for (i=2;i<=34000;i++)//First hit table {a[i]=a[i-1]+ (LL) log10 ((double) i) +1; S[i]=s[i-1]+a[i]; } scanf ("%lld", &CLA); while (cla--) {scanf ("%lld", &n); i=0; while (s[i]<n) i++; m=n-s[i-1];//find subscript i=0 in n in all sequences; while (M>a[i]) i++;//i is the length of the N Group in printf ("%lld\n", So (i,a[i]-m));//<span style= "Font-family:consolas, ' Co Urier New ', Courier, mono, serif; line-height:18px; Background-color:rgb (248, 248, 248); " > For example, to remove 1234 of 2, the extra bits have 2 bits: 34. Then use 1234 / 100, get 12, then 12 modulo 10, get 2</span>} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1019 Number Sequence (the nth value of the loop increment sequence)