UVa 10706:Number Sequence

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1647

原题:

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 80digits of the sequence are as follows:

11212312341234512345612345671234567812345678912345678910123456789101112345678910

Input

The first line of the input file contains a single integer t (1 <=t <=25), 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

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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

样例输入:

2

8

3

样例输出:

2

2

题目大意:

一个数字序列由S1S2…Sk组成, 其中Si也表示一个序列,为1,2,3....i.   输入一个数字i, 然后输出这个序列中的第i位上的数字是多少。

分析与总结:

太挫了,只想到了模拟+找规律的方法。

首先可以发现一些规律:

每个位数的开始数字分别为1,10,100,1000,10000……可以把这些开始数字作为一个分界线。

用一个数组num来保存, num【i】表示数字位数为1,2…i这些位数的总和。

那么利用这个数组来求一个Si序列的总位数就很好办了, 假设i=101, 那么Si就是数字序列1,2…101,它的数字位数总和Si=(i-digit+1)*+num[digit-1], 其中digit表示i这个数的位数。

然后开一个数组S,S【k】用来保存S1S2…Sk这个序列的总位数。

预处理好这些信息之后,就可以开始查找和模拟了。

要查找第i位上的数字,那么首先要先查找到它是属于哪个Si, 可以直接在S数组中二分查找下界,也就是查找到第一个>=i的。假设查找到的下标为k, 如果S[k]刚好等于i, 那么k这个数字刚好是Sk序列1,2,3……k的最后一个数,那么k%10就是答案了。

如果arr[k]>i,那么可以知道i是属于Si这个子序列之间的一个数。 可以继续算得m=i-S【k-1】,那么答案那个位数的数字就在Si中的第m位。

接下来就是模拟, 找出Si的第m位是多少。

可以根据num数组进一步缩小查找范围, 在num数组中查找到>=m的第一个下标,假设该下标为p, 如果num【p】==m, 说明那个位数正好处于分界线的最后一位,一定是9。

如果num【p】>m的话,那么可以算出那个位数处在上面表格中的哪个范围中,然后在那个范围中模拟算下去,一直到m位为止。求出第i位数字属于Si序列1,2,3……i中的哪个元素。具体还是看代码吧,文字真不太好表达。

代码:

/*
 * UVa: 10706 - Number Sequence
 * Time: 0.008s
 * Author: D_Double
 *
 */
#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAXN 31272
using namespace std;
long long S[MAXN];
int scale[9]={0,1,10,100,1000,10000,100000,1000000,10000000};
int num[9]={0,9,90,900,9000,90000,900000,9000000,90000000};  

inline int getDigit(int n){
    int cnt=0;
    while(n>0){
        ++cnt;
        n /= 10;
    }
    return cnt;
}  

inline void init(){
    for(int i=2; i<9; ++i){
        num[i] = num[i-1]+num[i]*i;
    }  

    // 计算出S1,S2,S3..S5
    // 并且求出它们的和,及S[i]=S1+S2+..+Si
    S[0]=0;
    for(int i=1; i<MAXN; ++i){
        int digit=getDigit(i);
        S[i] = (i-scale[digit]+1)*digit+num[digit-1];
        S[i] += S[i-1];
    }
}  

int main(){
    init();
    int T, n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        long long *p=lower_bound(S, S+MAXN, n);
        if(*p==n) printf("%d\n",(p-S)%10);
        else{
            int m=n-*(p-1);
            bool ok=false;
            int j;
            for(j=0; j<9; ++j){
                if(num[j]==m){
                    ok=true;
                    break;
                }
                else if(num[j]>m){
                    break;
                }
            }
            if(ok){
                printf("9\n");
                continue;
            }  

            int left=m-num[j-1]; // 还要多少次
            int cnt=j;
            int k;
            for(k=scale[j]; cnt<left; cnt += j,++k);;
            int time=cnt-left;
            while(time>0) {k/=10; time--;}
            printf("%d\n", k%10);
        }
    }
    return 0;
}

作者:csdn博客 shuangde800

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, include
, uva
, integer
, sequence
The
sequence、oracle sequence、mysql sequence、boot sequence、create sequence,以便于您获取更多的相关知识。

时间: 2024-08-30 16:19:06

UVa 10706:Number Sequence的相关文章

acm-杭电ACM OJ 1005 Number Sequence

问题描述 杭电ACM OJ 1005 Number Sequence A number sequence is defined as follows: f(1) = 1 f(2) = 1 f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A B and n you are to calculate the value of f(n). 超过49个数之后一定会出现和之前的数组合相同的情况,这个我可以了解,但是 为什么最多经过49个数之后一定会出现周

UVa 10706 / POJ 1019 Number Sequence:打表及O(1)算法

10706 - Number Sequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1647 http://poj.org/problem?id=1019 Description A single positive integer i is giv

uva 10706 - Number Sequence

点击打开链接uva 10706 题目意思:    有一个数组 s[1] = 1 , s[2] = 1 2 , .......s[k] = 1....k,要求给定一个n表示数组的第几位,要求这个第几位是什么数.例如 n为1 时候是1 n为 2 时候是1 ,n 为3 时候为2 解题思路:    1:思路:预处理打表+查找                      2:题目给的数据可以发现一些规律                         s[1]:1                      

UVa 10591:Happy Number

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1532 类型: 哈希表 原题: Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let

AliSQL情人节版本Release:开源Sequence Engine

AliSQL开源Sequence Engine Introduction 单调递增的唯一值,是在持久化数据库系统中常见的需求,无论是单节点中的业务主键,还是分布式系统中的全局唯一值,亦或是多系统中的幂等控制.不同的数据库系统有不同的实现方法,比如MySQL提供的AUTO_INCREMENT,Oracle,SQL Server提供SEQUENCE等. 在MySQL数据库中,如果业务系统希望封装唯一值,比如增加日期,用户等信息,AUTO_INCREMENT的方法会带来很大的不便,在实际的系统设计的时

[ACMcoder] Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDOJ 1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

UVa 694 The Collatz Sequence:数论

694 - The Collatz Sequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=635 An algorithm given by Lothar Collatz produces sequences of integers, and is

UVa 10624:Super Number, Rujia Liu的神题

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=112&page=show_problem&problem=1565 原题: Don't you think 162456723 very special? Look at the picture below if you are unable to find its speciality. (a | b mea