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 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

完整代码:

复杂度:O(n),但常数项很小

/*UVa: 0.016s*/
/*POJ: 0ms,648KB*/

#include <cstdio>
#include <cmath>

int a[31270], s[31270];

inline int pow_10(int x, int d)
{
    while (d--) x /= 10;
    return x % 10;
}

int main(void)
{
    int T, n, t, i;
    for (i = 1;; i++)
    {
        a[i] = a[i - 1] + (int)(log10((double)i)) + 1;
        s[i] = s[i - 1] + a[i];
        if (s[i] < 0) break;
    }
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        i = 0;
        while (s[i] >= 0 && s[i] < n) i++;///n所在的组
        t = n - s[i - 1];
        i = 0;
        while (a[i] < t) i++;///n所指向的数的个位数
        printf("%d\n", pow_10(i, a[i] - t));
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, while
, integer
, line
, sequence
, The
rev1019下载
poj1019、开心消消乐1019、k1019次列车时刻表、1019违章代码、k1019,以便于您获取更多的相关知识。

时间: 2025-01-17 19:26:06

UVa 10706 / POJ 1019 Number Sequence:打表及O(1)算法的相关文章

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 n

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                      

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个数之后一定会出现周

[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

printf-设有一个顺序表A,包含n个元素,要求写出一个将该表逆置的算法,

问题描述 设有一个顺序表A,包含n个元素,要求写出一个将该表逆置的算法, #include #define MaxLen 50 typedef int elemtype: typedef elemtype sqlist [MaxLen]: int create (sqlist A) { int i,n: printf("创建一个顺序表n"): printf("输入元素个数:"): scanf( ): for (i=0:i<n:i++) { printf(&qu

UVa 113 / POJ 2109 Power of Cryptography

使用double处理大整数&泰勒公式与误差分析 113 - Power of Cryptography Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49 http://poj.org/problem?id=2109 Background Curre

UVa 10038 / POJ 2575 / ZOJ 1879 Jolly Jumpers (water ver.)

10038 - Jolly Jumpers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=979 http://poj.org/problem?id=2575 http://acm.zju.edu.cn/onlinejudge/showProblem.do?

UVa 306 / POJ 1026 Cipher (置换群)

306 - Cipher Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=242 http://poj.org/problem?id=1026 Bob and Alice started to use a brand-new encoding scheme.