hihocoder上第180周《Nature Numbers》


原题链接:http://hihocoder.com/contest/hiho180/problems

描述

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

Solution:

本题属于数学问题,可以按照每一个数的位数来加快计算,例如,个位数有10,2位数90,3位数900,依次类推即可。代码如下:
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;

int main() {
    ll N;
    while (cin >> N) {
        ll n = N;           // 剩余位数
        ll k = 1;           // 几位数
        ll nums = 10;       // 该k位数有多少个
        while (n - k * nums >= 0) {
            n -= k * nums;
            k++;
            if (k == 2) nums *= 9;
            else nums *= 10;
        }
        ll m = k == 1 ? 0 : pow(10, k - 1); // k位数的起始数
        ll nn = n / k + m;      // 第N个字符所在数的大小
        ll mm = n % k;          // 第几位
        cout << (nn % ll(pow(10, k - mm))) 
            / ll(pow(10, k - mm - 1)) << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值