hihocoder#1349 : Nature Numbers(思维)

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

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
思路:易知1位数有9个(不包括0),2位数有90个,3位数有900个,4位数有9000个,以此类推。。。可以算出s[n]在几位数里面。然后算出s[n]在哪个整数里面。答案就出来了。

#include
using namespace std;
int main()
{
	long long n,s,num,i,ans;
	cin>>n;
	if(n==0)puts("0");
	else
    {
        s=0,num=9,i=1;//i代表位数,num代表i位数有多少个
        while(n>=s+num*i)
        {
            s+=num*i;
            i++;
            num*=10;
        }
        n-=s;
        num=(num/9)-1;
        num+=n/i;//num代表s[n]所在的整数
        n%=i;
        if(n==0)printf("%d\n",num%10);
        else
        {
            num++;
            stackp;
            while(num)p.push(num%10),num/=10;
            while(n)
            {
                ans=p.top();
                p.pop();
                n--;
            }
            cout<



你可能感兴趣的:(思维)