POJ 1019  Number Sequence

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

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
额……这个题原来做过的, POJ <wbr>1019 <wbr> <wbr>Number <wbr>Sequence
代码和测试数据如下:
#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值