poj1019 Number Sequence

来源:互联网 发布:禁忌的边界线知乎 编辑:程序博客网 时间:2024/05/01 08:49
Number Sequence
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 34632 Accepted: 9949

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

283

Sample Output

22

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest


题意:给你一串112123...这样的数列,然后让你求第n位的数字是多少。

做法:参考了人家大神的做法,首先知道后一窜数列比前一串数列多了log10(num)+1位,所以先预运算好,然后去找到n在那个数列中,找到之后就能做了。(更加详情的看代码。)

#include <iostream>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include<map>#include <algorithm>#include<ctime>#define esp 1e-6#define LL long long#define inf 0x0f0f0f0fusing namespace std;int main(){    long long a[50000];    long long s[50000],t,pos,ans;    long long n,i,tt;    a[1]=1;    s[1]=1;    for(i=2;i<=38000;i++)    {        a[i]=a[i-1]+(int)log10((double)i)+1;        s[i]=s[i-1]+a[i];    }    scanf("%lld",&tt);    while(tt--)    {        scanf("%lld",&n);        i=1;        while(n>s[i])        {            i++;        }        pos=n-s[i-1];        t=0;        for(i=1;t<pos;i++)        {            t+=(int)log10((double)i)+1;        }        pos=t-pos;        ans=(i-1)/(int)pow(10.0,(double)pos)%10;        printf("%lld\n",ans);    }    return 0;}


0 0
原创粉丝点击