Uva--10706(技巧)

2014-07-23 20:48:32

Problem B
Number Sequence
Input: standard input
Output: standard output
Time Limit: 1 second

A single positive integer iis given. Write a program to find the digit located in the position iin the sequence of number groups S1S2…Sk. Each groupSkconsists 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 <=25), 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                           Output for Sample Input

2

8

3

2

2


Problem source: Iranian Contest

Special Thanks: Shahriar Manzoor, EPS.

思路:技巧题,纯考验思维和编程能力。

 1 /*************************************************************************
 2     > File Name: Uva10706.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Tue 22 Jul 2014 09:05:20 PM CST
 6  ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int main(){
17    int Case,n,p,num[100000];
18    scanf("%d",&Case);
19    while(Case--){
20        scanf("%d",&n);
21        for(int i = 1; i < 10; ++i)
22            num[i] = num[i - 1] + 1;
23        for(int i = 10; i < 100; ++i)
24            num[i] = num[i - 1] + 2;
25        for(int i = 100; i < 1000; ++i)
26            num[i] = num[i - 1] + 3;
27        for(int i = 1000; i < 10000; ++i)
28            num[i] = num[i - 1] + 4;
29        for(int i = 10000; i < 100000; ++i)
30            num[i] = num[i - 1] + 5;
31        int k;
32        for(k = 1; ; ++k){
33            if(n - num[k] <= 0)
34                break;
35            n -= num[k];
36        }
37        int l = 1,r = 100000,mid;
38        while(r > l + 1){
39            mid = (l + r) / 2;
40            if(num[mid] >= n) r = mid;
41            else l = mid;
42        }
43        n -= num[l];
44        //printf("n : %d , l : %d r : %d\n",n,l,r);
45        if(n == 0) printf("%d\n",l % 10);
46        else{
47             int ans[100],cnt = 1;
48             while(r){
49                 ans[cnt++] = r % 10;
50                 r /= 10;
51             }
52             printf("%d\n",ans[cnt - n]);
53         }
54     }
55    return 0;
56 }
posted @ 2014-07-23 20:49  Naturain  阅读(297)  评论(0编辑  收藏  举报