Digit | ||
Accepted : 82 | Submit : 301 | |
Time Limit : 1000 MS | Memory Limit : 65536 KB |
题目描述我们把十进制整数依次写成一个字符串,123456789101112…请问第n位数码是多少? 输入第一行是一个整数T(T≤10000),表示样例的个数。 每行输入一个整数n(1≤n≤788888899)。 输出每行输出一个样例的结果。 样例输入2 1 788888899 样例输出1 1 |
/*
链接:http://202.197.224.59/OnlineJudge2/index.php/Contest/read_problem/cid/38/pid/1227
题意:如题。
分析:从低位往高位找,找出第N个数码所在的数字。复杂度O(1)。
写给自己:个人解这道题的逻辑有点复杂,适宜多写一点注释,调试时试遍一类数据的所有情况。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 100005;
typedef long long LL;
LL a[15];
LL N;
void init()
{
LL l = 1, r = 9;
for (int i = 1; i<15; i++)
{
a[i] = (r - l + 1)*i;
l *= 10;
r = r * 10 + 9;
}
}
int main()
{
//freopen("f:\\input.txt","r",stdin);
init();
int T;
scanf("%d", &T);
while (T--)
{
scanf("%lld", &N);
if (1 <= N&&N <= 9)//对一位数特判
{
printf("%lld\n", N);
continue;
}
LL sum = 0;
int cnt = 1;
for (int i = 1; i<15; i++)
{
sum += a[i];
if (sum >= N)
{
cnt = i;
sum = sum - a[i];
break;
}
}
LL st = (LL)(pow(10, cnt - 1) + 0.5);//数位起点
LL num = (N - (sum + 1)) / cnt;//答案在从st开始的第num个数
LL ret = st + num;//答案在ret中
char c[15];
sprintf(c, "%lld", ret);
LL tmp = (N - (sum + 1)) % cnt;
printf("%c\n", c[tmp]);
}
return 0;
}