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