Number Sequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36256   Accepted: 10461

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

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
 
题意就不说了,看下就明白。思路就是打表,然后就是要知道求一个数的位数:(int)log10((double)x)+1
 
/*
ID: LinKArftc
PROG: 1019.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; ll sum[maxn], line[maxn];//分别是前i行的位数和,第i行的位数 int getbit(int x) {
return (int)log10((double)x) + ;
} void init() {
sum[] = line[] = ;
for (int i = ; i <= ; i ++) {
line[i] = line[i-] + getbit(i);
sum[i] = sum[i-] + line[i];
}
} int getpos(int x, int pos) {
int len = getbit(x);
for (int i = ; i <= len - pos; i ++) x /= ;
return x % ;
} int main() { init();
int T;
ll n;
scanf("%d", &T);
while (T --) {
scanf("%lld", &n);
int cur = ;
while (sum[cur] < n) cur ++;
ll pos = n - sum[cur-];
while (cur >= ) {
if (pos > line[cur-]) {
pos -= line[cur-];
printf("%d\n", getpos(cur, pos));
break;
} else cur --;
}
} return ;
}
 
 

最新文章

  1. Amazon Interview | Set 27
  2. docker学习(7) docker-compose使用示例
  3. Range Sum Query - Immutable
  4. 【Unity3D】计算二维向量夹角(-180到180)
  5. Raspberry Pi 3 Model B 安装 OSMC
  6. two day python基础知识
  7. java List 排序 Collections.sort() 对 List 排序
  8. Java调第三方的webservice接口
  9. java面板
  10. Unity3d Shader开发(四)UsePass ,GrabPass ,SubShader Tags
  11. 动态规划——C编辑最短距离
  12. Spring 学习笔记01
  13. Unity sqlite学习笔记一
  14. python3 selenium 登录操作
  15. Velocity(7)——velocity进阶用法
  16. 异常处理--logging模块
  17. 【转】一文掌握 Linux 性能分析之内存篇
  18. js对json解析获取对应属性的值,JSON.stringify()和JSON.parse()
  19. git回答整理
  20. Android实现动态改变屏幕方向(Landscape &amp; Portrait)

热门文章

  1. 第二十二篇 正在表达式 re模块
  2. Django源码分析之权限系统_擒贼先擒王
  3. MySQL训练营03
  4. EasyUI 显示表单数据 小记
  5. 从微软msdn阅读事件的使用
  6. 进程id
  7. [BinaryTree] 二叉搜索树(二叉查找树、二叉排序树)
  8. oracle补充
  9. DataBase -- Customers Who Never Order
  10. VS查看DLL接口