zoukankan      html  css  js  c++  java
  • E

    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; 
    }
  • 相关阅读:
    InitializingBean
    执行jar的记事本
    vue中$forceUpdate()事件
    帆软时间检索限制90天
    vue清楚子组件v-model绑定的值
    Intellij IDEA中启动多个微服务(开启Run Dashboard管理)
    Java将CST的时间字符串转换成需要的日期格式字符串
    axios发送命令如何实现同步
    利用tomcat启动web前端
    vue生命周期
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5186782.html
Copyright © 2011-2022 走看看