E - Number Sequence

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

这道题不打表会超时 ,以下 超时代码
#include<iostream>
#include<cmath>
using namespace std;
void f(int n)
{
    int p=1,j=1,t;
    bool flag;
    while(1){
        flag=false;
        for(j=0;j<=p;j++){
            int i;
            for(i=0;;i++){
                t=pow((double)10,i);
                if(j/t==0){
                    n-=i;
                    break;
                }
            }
            if(n<=0){
                int k=i+n;
                flag=true;
                n=j%(int)(pow((double)10,i-k+1))/pow((double)10,i-k);
                break;
            }
        }
        if(flag==true)break;
        p++;
    }
    cout<<n<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        f(n);
    }
    //system("pause");
    return 0;
}

打表后!!!这个代码不好理解

#include<iostream>
#include<cmath>
using namespace std;
unsigned int a[31270],s[31270];
void f()
{
    int i;
    a[1]=1;
    s[1]=1;
    for(i=2;i<31270;i++)
    {
        a[i]=a[i-1]+(int)log10((double)i)+1;   //记录1至s[i]个数字的位数和
        s[i]=s[i-1]+a[i];                      //一位 记录 1至s[i]个数字
    }
}
int main()
{
    int t;
    int n;
    int i;
    cin>>t;
    f();
    while(t--)
    {
        cin>>n;
        i=1;
        while(s[i]<n) i++;
        int pos=n-s[i-1];
        int tmp=0;
        for(i=1;tmp<pos;i++)           //第n个数字在s[i-1]这个数据组中
        {
            tmp+=(int)log10((double)i)+1;
        }
        int k=tmp-pos;       //数字i从低位数的第k+1位
        cout<<(i-1)/(int)pow(10.0,k)%10<<endl;
    }
    return 0;
}
时间: 2024-12-28 20:55:28

E - Number Sequence的相关文章

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

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

TOJ 1203: Number Sequence

1203: Number Sequence Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte Total Submit: 957            Accepted:208 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. Gi

ACM—Number Sequence(HDOJ1005)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 主要内容: 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). 看到这样的公式很容易想到递归调用求解,但是在本题中n的取

HDU 1711 Number Sequence(KMP算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15548    Accepted Submission(s): 6836 Problem Description Given two sequence

Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j ( i

poj 1019 Number Sequence 二分

Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34391   Accepted: 9879 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...

HDU - 5014 Number Sequence

Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows("⊕" deno

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100