Number Sequence
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24188 | Accepted: 6468 |
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:
112123123412345123456123 456712345678123456789123
456789101234567891011123
45678910
For example, the first 80 digits of the sequence are as follows:
112123123412345123456123
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
额……这个题原来做过的,
代码和测试数据如下:
#include<stdio.h>
int count(int n)
{
if(n<10)return n;
else if(n>=10&&n<=99)
return 9+(n-9)*2;
else if(n>=100&&n<=999)
return 189+(n-99)*3;
else if(n>=1000&&n<=9999)
return 2889+(n-999)*4;
else if(n>=10000&&n<=99999)
return 38889+(n-9999)*5;
return -1000000000;
}
int main()
{
int i,n,m,a[100000];
char s[10];
for(i=1;i<=99999;i++)
a[i]=count(i);
scanf("%d",&m);
while(m--)
{
scanf("%d",&n);
for(i=1;;i++)
{
if(a[i]>n)break;
else n=n-a[i];
}
if(n==0){printf("%d\n",(i-1));continue;}
for(i=1;i<=99999;i++)
if(n>=a[i]&&n<a[i+1])
{
n=n-a[i];
break;
}
if(n==0)printf("%d\n",i);
else{
sprintf(s,"%d",i+1);
printf("%c\n",s[n-1]);
}
}
return 0;
}
数据:
1~1,2~1,3~2,10~4,11~1,45~9
46~1,47~2,56~0,57~1,80~0,9044~9,9045~9
9046~1,9235~1,9236~0,9236~0,1395493~9,1395494~9,1395495~9
(1395495+1)~1,(1395495+2893-3)~1,(1395495+2893-2)~0,
(1395495+2893-1)~0,(1395495+2893)~0,(1398389)~1,(189414495)~9,
(189414495-1)~9,(189414495-2)~9,(189414495-3)~9
189453389~0,189453388~0,189453387~0,
189453386~0,189453385~1,189453390~1,2147483647~2
额……这个题原来做过的,
![POJ <wbr>1019 <wbr> <wbr>Number <wbr>Sequence POJ <wbr>1019 <wbr> <wbr>Number <wbr>Sequence](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
代码和测试数据如下:
#include<stdio.h>
int count(int n)
{
}
int main()
{
}
数据:
1~1,2~1,3~2,10~4,11~1,45~9
46~1,47~2,56~0,57~1,80~0,9044~9,9045~9
9046~1,9235~1,9236~0,9236~0,1395493~9,1395494~9,1395495~9
(1395495+1)~1,(1395495+2893-3)~1,(1395495+2893-2)~0,
(1395495+2893-1)~0,(1395495+2893)~0,(1398389)~1,(189414495)~9,
(189414495-1)~9,(189414495-2)~9,(189414495-3)~9
189453389~0,189453388~0,189453387~0,
189453386~0,189453385~1,189453390~1,2147483647~2