首先说一下题意:给你一个数列,他是形如 1121231234123456123456712345678123……k。的数列,题意是让求出第几位数字是几。比如第一位是1,第80位是0。并不是说第八十个是几。
把数列进行分组:
1
12
123
1234
12345
……这样分组。
解题思路其实就是打表+推理,打表很简单就是算出前第几组时已经有多少个数字了。
然后就是分析,找到当前输入的n位于哪两个组之间,如果正好是这个组的末尾就输出k%10的结果,就是结果。如果不是的话,就找到位于两个数组的哪个位置,重点是判断位置,因为比如12是占两位的。所以用long10(i)经行递增。找到位置然后判断一下输出结果就OK了啊、、
Number Sequence
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31675 | Accepted: 9001 |
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
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <cmath>
#include <math.h>
#define maxn 65537
long long sum[65537], a[65537];
using namespace std;
void _printf()
{
a[0] = sum[0] = 0;
int i;
for(i = 1; i < maxn; i++)
a[i] = a[i-1]+(int)log10(1.0*i) +1;
for(i = 1; i < maxn; i++)
sum[i] = sum[i-1]+a[i];
}
int main()
{
int t, i;
long long n;
_printf();
cin >>t;
while(t--)
{
cin >>n;
int k;
for(i = 1; i < maxn; i++)
{
if(n <= sum[i])
{
k = i;
break;
}
}
if(sum[k] == n)
{
cout <<k%10<<endl;
continue;
}
int tt = 0;
int kk = n-sum[k-1];
for(i = 1; tt < kk; ++i)
{
tt += ((int)log10(1.0*i)+1);
}
cout <<(((i - 1)/(int)pow(10*1.0,1.0*(tt - kk)))%10)<<endl;
}
return 0;
}