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
Source
Tehran 2002, first Iran Nationwide Internet programming Contest
The main idea is to give you this string of numbers 11212312341234512345612345671234567812345678912345678910123456789101112345678910 ... (not finished)
We want to find out how many nth numbers (from left to right), such as the 2nd is 1, the third is 2, and the eighth one is 2;
If you look closely at this string of numbers, you can find that he can also be divided into a lot of small strings, assuming that the first small string is 123......i, assuming that the space occupied by the small string is a[i], then by contrast a[i] and a[i+1] found,
Part I The +1 string is only one more number than the string I, that is, the i+1, so they occupy the space of i+1.
The space occupied by any number is very good, that is (int) log10 (k) +1;
Then we can find out the starting position of each string, by comparing with N can be determined that n appears in that string, and finally in finding the relative position of n in this string, we can find out the solution of the problem
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <bitset># Include <algorithm> #include <climits>using namespace std; #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = ACOs ( -1.0); #define N 32000#define mod 19999997#define INF 0x3f3f3f3f#define exp 1e-8 int a[40000],num[1000000],l; LL S[40000];int Main () {int t,n,i,j,k; A[0] = 0; A[1] = 1; Up (i,2,n) {a[i]=a[i-1]+ (int) log10 (1.0*i) +1;//plus the number of bits per number, the length of the I string} s[0] = 1; Up (I,1,n)//calculates the coordinates of the start of the I string {s[i]=s[i-1]+a[i-1]; } L = 1; Up (i,1,n)//calculate the longest string is what {int bit[50]; t = i; int len = 0; W (t) {int r = t%10;bit[len++] = r; t/=10; } w (len--) {num[l++] = Bit[len]; }} scanf ("%d", &t); W (t--) {scanf ("%d", &n); Up (I,1,n) {if (s[i]>=n) break; } if (S[i]==n)//Is the beginning of a string printf ("1\n"); else//Find location printf ("%d\n", num[n-s[i-1]+1]); } return 0;}
Poj1019:number Sequence