zoukankan      html  css  js  c++  java
  • POJ 1019 Number Sequence

    找规律,先找属于第几个循环,再找属于第几个数的第几位。。。。。。

    Number Sequence
    Time Limit: 1000MSMemory Limit: 10000K
    Total Submissions: 31552Accepted: 8963

    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 



    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>

    using namespace std;

    inline int getlen(int x)
    {
        return log10(1.0*x)+1;
    }

    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int pos,th,kth=0,x=1,nth=0,i;
            scanf("%d",&pos); th=pos;
            while(pos>0)
            {
                kth+=getlen(x);
                pos-=kth;
                x++;
            }
            x=x-1; pos=pos+kth;
            for(i=1;i<=x;i++)
            {
                nth+=getlen(i);
                if(nth>=pos)
                    break;
            }
            nth-=getlen(i);
            int deta=pos-nth;
            int bit[10],ii=0;
            while(i)
            {
                bit[ii++]=i%10;;
                i/=10;
            }
            printf("%d ",bit[ii-deta]);
        }
        return 0;
    }

    /*   有爱的测试数据。。。
                1
                1
                2
                1
                2//5
                3
                1
                2
                3
                4//10
                1
                2
                3
                4
                5//15
                1
                2
                3
                4
                5//20
                6
                1
                2
                3
                4//25
                5
                6
                7
                1
                2//30
                3
                4
                5
                6
                7//35
                8
                1
                2
                3
                4//40
                5
                6
                7
                8
                9//45
                1
                2
                3
                4
                5//50
                6
                7
                8
                9
                1//55
                0
                1
                2
                3
                4//60
                5
                6
                7
                8
                9//65
                1
                0
                1
                1
                1//70
                2
                3
                4
                5
                6//75
                7
                8
                9
                1
                0//80
    */

    * This source code was highlighted by YcdoiT. ( style: Codeblocks )
  • 相关阅读:
    array_values — 返回数组中所有的值
    array_unshift — 在数组开头插入一个或多个单元
    array_unique — 移除数组中重复的值
    array_uintersect — 计算数组的交集,用回调函数比较数据
    array_uintersect_uassoc — 带索引检查计算数组的交集,用单独的回调函数比较数据和索引
    职场中的起跑线上,从赢在一个办公邮箱开始
    可终身使用的会员邮箱靓号,到底有多酷?
    商务业务人员,用什么邮箱更能获得认可?
    电子邮箱有哪些类型,2020什么邮箱最火爆?
    外贸邮箱用哪个比较好?企业域名邮箱注册哪个好?
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350813.html
Copyright © 2011-2022 走看看