zoukankan      html  css  js  c++  java
  • 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 <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <stdlib.h>
    //log10(n)就可以求出n的位数
    #define LEN 32000
    unsigned int a[LEN];
    unsigned int b[LEN];
    int main()
    {
        a[0] = 0;
        a[1] = 1;
        b[0] = 0;
        b[1] = 1;
        int i;
        for(i = 2; i < LEN; i++)
        {
            a[i] = a[i-1] + log10((double)i) + 1;
            b[i] = b[i-1] + a[i];
        }
    
        unsigned int cases;
        unsigned int n;
        scanf("%u", &cases);
        while(cases--)
        {
            scanf("%u", &n);
            unsigned int j;
            for(j = 0; j < LEN; j++)
                if(n <= b[j])
                    break;
            //在字串中的位置
            unsigned int remain = n - b[j-1];
    
            unsigned int value;
            unsigned int sum = 0;
            for(value = 1; value < LEN; value++)
            {
                sum += 1+log10((double)value);
                if(sum >= remain)
                    break;
            }
            //value的位数
            unsigned int value_bits = 1+log10((double)value);
            unsigned int position = remain - (sum - value_bits);
            //求value的第position位
            char* str = (char*)malloc(10*sizeof(char));
            int temp = value;
            int stri = 0;
            while(temp)
            {
                int temp1 = temp%10;
                str[stri++] = temp1 + '0';
                temp = temp/10;
                
            }
            str[stri] = '';
            if(position == value_bits)
                printf("%c
    ", str[0]);
            else
                printf("%c
    ", str[position - 1]);
        }
        return 0;
    }
     
    选择了远方,便只顾风雨兼程
  • 相关阅读:
    微信小程序wx.request请求用POST后台得不到传递数据
    小程序的movable-view怎么持续移动
    当inline-block或者float失效的时候怎么弄
    js中如何删除json对象的某一个选项
    情非得已
    框架变量的问题
    隐式等待写法__和显示等待对比问题___及误区
    显式等待大结局___封装成API方便控制层调用
    显式等待第二集____灵活写法__
    显式等待__第一集___追加了误区
  • 原文地址:https://www.cnblogs.com/ly-rabbit-wust/p/5575150.html
Copyright © 2011-2022 走看看