poj1019 Number Sequence

本文详细解析了如何通过算法定位给定数字序列中的特定位置数字,包括分组、最大分组构建及查找方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Number Sequence
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 34343 Accepted: 9860

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

2
8
3

Sample Output

2
2

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest


大致题意:

有一串数字串,其规律为

1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112······k

输入位置n,计算这一串数字第n位是什么数字,注意是数字,不是数!例如12345678910的第10位是1,而不是10,第11位是0,也不是10。总之多位的数在序列中要被拆分为几位数字,一个数字对应一位。

注意:2147483647刚好是int的正整数最大极限值( ),所以对于n用int定义就足矣。但是s[31268]存在超过2147483647的位数,因此要用unsigned 或long 之类的去定义s[]。-----我用的unsigned int;

解析: 在我A掉这道题后,参看了别的大牛的博客,其他人都用了很强的数学技巧,而我却没有。到也不这么认为。

          先分组  1, 12, 123 , 1234,12345,。。。K  为一组,利用一个a数组记录 该组有多少位  

          打出最大的[2147483647]分组,里面的值,在这些组中前缀都是一样的,所以我们就打出最大的一组。

          题目给一个n,累加数组 a[],找到所在数组的前一个,输出 在第二个表的位置。

         具体见代码。。个人认为挺简单的。。

/*  整个世界也填满不了十八岁男孩子的雄心和梦  */
/*                             By---宇        */
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <limits.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#define N 31268
#define mem(a) memset(a,0,sizeof(a));
#define mem_1(a) memset(a,-1,sizeof(a));
using namespace std;
unsigned int   a[N+10];     //分组
unsigned int ans[N*5+10];    //31268的表
unsigned int  f[5]= {1,10,100,1000,10000};
void Init()     //模拟分组 + 打出第31268的表
{
    unsigned int  t,i,j;
/*******模拟分组 把1看做第一组,12看做第2组,123看做第3组 等等*******/
    for(i=1; i<=31268; i++)     
    {
        t = i;
        if(t<=9) a[i] = t;
        else if(t<=99) a[i] = (t-9)*2+9;
        else if(t<=999) a[i] = (t-99)*3+90*2+9;
        else if(t<=9999) a[i] = (t-999)*4+900*3+90*2+9;
        else a[i] = (t-9999)*5+9000*4+900*3+90*2+9;
    }
    
/*****************打出31268的表****************/
    unsigned int cnt = 1,flag;
    for(i=1; i<=31268; i++)       
    {
        flag = 0;
        for(j=5; j>=1; j--)
        {
            t = i/f[j-1];
            t = t%10;
            if(t||flag)
            {
                ans[cnt++] = t;
                flag = 1;
            }
        }
    }
}

int main()
{
    unsigned int  T,i;
    long long n;
    Init();
    cin >> T;
    while(T--)
    {
        cin >> n;
        long long sum = 0;
        for(i=1;; i++)             //寻找在第几个分组的前一组
        {
            sum+=a[i];
            if(sum>=n)break;
        }
        n = n-(sum-a[i]);          //找到该分组的第几位
        cout << ans[n]<< endl;;    //从表里输出该位
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值