POJ 1019 Number Sequence

来源:互联网 发布:单片机控制继电器 编辑:程序博客网 时间:2024/06/05 01:41

描述

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

输入

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)

输出

There should be one output line per test case containing the digit located in the position i.

样例输入

2
8
3

样例输出

2
2


题目跟之前的不大一样 之前是1-9的重复  这个是1-N的来
数学问题   转载的。。方法很简单 动笔写写就容易明白

#include<iostream>#include<cstdio>#include<cmath>using namespace std;const int maxn=31269;unsigned a[maxn];unsigned s[maxn];void Set(){     a[1]=1,s[1]=1;    for(int i=2;i<maxn;i++){        a[i]=a[i-1]+(int)log10((double)i)+1;            s[i]=s[i-1]+a[i];//表示第i组数字列的长度    }   }int Compute(int n){    int i=1;    while(s[i]<n)        i++;    int pos=n-s[i-1];//答案在s[i-1]里的第pos个    int len=0;    for(i=1;len<pos;i++)        len+=(int)log10((double)i)+1;//找到对应len的位置    return (i-1)/(int)pow((double)10,len-pos)%10;//此时的i-1便是具体的值 答案在i里  去除尾巴}//取出1234的2,那么多余的位数有2位:34。那么用1234 / 10^2,得到12,再对12取模10,就得到2int main(){    int t,n;    scanf("%d",&t);    Set();    while(t--){        scanf("%d",&n);        printf("%d\n",Compute(n));    }    return 0;}


0 0
原创粉丝点击