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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
    题目大致意思:有一些数按照一定的规律 ex:1 12 123 1234 12345 123456 ...... 排列,让后给出一个n,求第n个数字是什么。
    分析:
    这是我第一次交的WA代码
#include <iostream>

using namespace std;

long long int a[65539];

int main()
{
    int t;
    long long int n;
    for (int i = 1; i <= 65538; i++)
        a[i] = i;
    cin >> t;
    while (t--)
    {
        cin >> n;
        int i = 1;
        while (n-a[i]>0)
        {
            n -= a[i];
            i++;
        }
        cout << n << endl;
    }
}

    我发现其实那些数都是首相为1,公差为1的等差数列(个数)1 12 123 1234 ......   --->  1 2 3 4 ......

    那么第n个数列的个数就是(n2+n)/2

    那么第n个数就应该是减去前面数字和以后的数,(n始终大于0)

    ex:如果n是8  那么第n个数就是 n-1-2-3-4-5......  ---> 8-1-2-3=2  (不能再减4了,再减就小于0了)

    可以说在某种程度上是对的,但是我忽略了一个严重的问题,就是:第n个数字,恩...就是一个数字而已,但是我上述的做法在n大一点的情况下就会出现多个数字的情况,而且统计数字也不正确,ex:12345678910  这个序列我统计是有10个数字 但是实际上应该是11个数字

    其实那些超过9的就是一些特殊情况,因为他们可能是2 3 4 5....位数,比如12是两位数,123是三位数,1234是四位数......我们可以发现一个很明显的规律:12的高位是十位,123的高位是百位,1234的高位是千位,对应的他们比高位是个位的数字多了1 2 3个数字,所以我们可以对这些数求以十为底的对数,这样我们统计数字就不会出错了
    数位问题解决以后又有另外一个问题:
    给出的n是第n个数字,我们很容易确定这第n个数字在第几个序列里面,可是,我们很可能会遇到这种情况:如果我的n确定是在第i个数组里,然后我又确定是第i个数组里第pos个数字,那么我怎么知道第pos个数字是什么呢?
    ex:我们假设确定的第i个序列是123456789,而我们需要第5个数字,那么我们只需要把6789去掉,然后再对十求余就好
 
代码如下:
#include <iostream>
#include <cmath>

using namespace std;

__int64 a[34001];
__int64 s[34001];

void Init()                               //打表 
{
    a[1] = s[1] = 1;
    for (int i = 2; i <= 34000; i++)
    {
        a[i] = a[i - 1] + (__int64)log10((double)i) + 1;
        s[i] += s[i - 1] + a[i];                         //记录前i个数的数位 方便定位
    }
}

void print(__int64 x, __int64 y)      //输出结果
{                                                      //例如123456789 我们还剩余5位  应该输出5  那么我们就把后面的4位去掉再求余
    while (y--)
        x /= 10;
    cout << x % 10 << endl;
}

int main()
{
    Init();
    int t;
    __int64 n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        __int64 i = 1;
        while (s[i] < n) i++;
        int pos = n - s[i - 1];
        i = 0;
        while (pos > a[i]) i++;
        print(i, a[i] - pos);
    }
}

 

    

转载于:https://www.cnblogs.com/LHJL8023/p/8004939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值