Description
A single positive 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 integer numbers ranging from 1 to k, written one after another.
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
For example, the first 80 digits of the sequence are 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 be one output line per test case containing the digit located in the position i.
Sample Input
2 8 3
Sample Output
2 2
Source
Tehran 2002, First Iran Nationwide Internet Programming Contest
题目大意就是给你这一串数字11212312341234512345612345671234567812345678912345678910123456789101112345678910……(未列完)
要我们求出第n个数是多少(从左到右看),例如第2个是1,第三个是2,第八个是2;
如果仔细观察这一串数字,可以发现他可以还分为很多小串,假设第i小串是123……i,假设第i小串所占的空间是a[i],则通过对比a[i]与a[i+1]发现,
第i+1串只比第i串多一个数,即i+1,故他们所占的空间差就是第i+1所占的空间。
对任意一个数所占的空间很好求,即 (int)log10(k)+1;
然后就可以求出每一个串的起始位置,通过与n比较就可以确定n出现在那一个串里,最后在求出n在这个串里的相对位置,就可以求出该题的解
#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 long
const 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;//加上每个数字所占得位数,得到第i个串的长度
}
s[0] = 1;
up(i,1,N)//计算得到第i个串起始的坐标
{
s[i]=s[i-1]+a[i-1];
}
l = 1;
up(i,1,N)//计算出最长的那个串是怎样的
{
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)//是一个串的开头
printf("1\n");
else//找到位置
printf("%d\n",num[n-s[i-1]+1]);
}
return 0;
}