HihoCoder 1349 Nature Numbers

题目链接


Consider the following sequence S which is constrcuted by writting nature numbers one by one: "012345678910111213...".

The first digit of S, S[0], is 0. The second digit S[1] is 1. And the 11th digit S[10] is 1.

Given an integer N, can you find the digit S[N]?

输入
An integer N. (0 <= N <= 1018)

输出
Digit S[N].

样例输入
17
样例输出
3

思路:
可以找到一位数所占的位数,二位数所占的位数......
这样首先可以知道要查询的数是在k位数中的,然后可以知道在k位数中的第m个,接着可以计算在k位数中第m个数的第i位(从左往右数)

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
const LL UP = 1e18;
LL fac[20]={1,1};
LL bar[20];
int digit[20];
LL n;
int main()
{
    for(int i = 2; i < 18; i++) fac[i] = fac[i-1] * 10;
    LL sum = 0;
    for(int i = 1; i < 18; i++)
    {
        bar[i] = fac[i]*9*i;
    }
    bar[1] = 9;
    while(cin >> n)
    {
        int dig = 1;
        while(n > bar[dig])
        {
            n -= bar[dig];
            dig++;
        }
        int num = n / dig + (n % dig != 0);
        int weishu = n % dig == 0? dig : n%dig;
        num = fac[dig] + num-1;
        int len = 0;
        while(num)
        {
            digit[len++] = num % 10;
            num /= 10;
        }
        int ans = 0;
        for(int i = 0; i < weishu; i++)
            ans = digit[--len];
        cout << ans << endl;
    }
    return 0;
}

就当自己又回来了吧

转载于:https://www.cnblogs.com/Alruddy/p/8040312.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值