北大oj1019——Sequence

Number Sequence

Time Limit: 1000MS Memory Limit: 10000K

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

我的解法思路是没问题的,就是边界问题花了不少时间,o(╥﹏╥)o

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

//寻找该位置在该一列中所在的位置
int calcRealIndex(int startPos, int addCnt, int sIns, int ins, double index) {
    if (ins == sIns) {
        return index;
    }
    //2分法查找
    double banNum = (ins - sIns) / 2 + sIns;

    long long tempCnt1 = 2 * startPos;
    long long tempCnt2 = addCnt * (banNum + sIns);
    long long tempCnt3 = tempCnt1 + tempCnt2;
    long long tempCnt = tempCnt3 * (1.0 * banNum / 2);

    if (tempCnt >= index) {
        return calcRealIndex(startPos, addCnt, 1, banNum, index);
    }
    else {
        return calcRealIndex(startPos + banNum * addCnt, addCnt, 1, ins - banNum, index - tempCnt);
    }
}

//通过该列中的位置寻找真正的值
int calcRealNum(int index) {
    for (int i = 1; ; i++) {
        int ins = pow(10.0, i) - pow(10.0, i - 1);
        if (index <= ins * i) {
            int realNum = ((index - 1) / i) + pow(10.0, i - 1);
            char s[20];
            sprintf_s(s, "%d", realNum);
            return s[(index - 1) % i] - '0';
        }
        index -= ins * i;
    }
}

int main() {
    int t;
    cin >> t;
    for (int _t = 0; _t < t; _t++) {
        int i;
        cin >> i;

        long long cnt = 0;
        int startPos = 0;
        int addCnt = 1;
        for (; cnt <= 2147483647; addCnt++) {
            int ins = pow(10.0, addCnt) - pow(10.0, addCnt - 1);
            long long tempAddCnt = (startPos + addCnt + startPos + addCnt * ins) * (1.0 * ins / 2);
            if (cnt + tempAddCnt >=i) {
                int realIndex = calcRealIndex(startPos, addCnt, 1, ins, i - cnt);
                int max = cnt + tempAddCnt;
                cout << calcRealNum(realIndex) << endl;
                break;
            }
                        //往上就越界了,直接写死结束
            else if (cnt + tempAddCnt >= 189414495) {
                cnt += tempAddCnt;
                startPos += ins * addCnt;
                addCnt++;
                int realIndex = calcRealIndex(startPos, addCnt, 1, 50000, i - cnt);
                cout << calcRealNum(realIndex) << endl;
                break;
            }
            cnt += tempAddCnt;
            startPos += ins * addCnt;
        }
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰子糖莫莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值