【小米OJ-找到第N个数字Ⅱ】

【小米OJ-找到第N个数字Ⅱ】

 

 

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
string now;
ll n;
int main()
{
    while(~scanf("%lld",&n))
    {
        now="";
        ll m=1;
        ll sum=0;
        while(1)
        {
            now+=std::to_string(m);
            m++;
            if(sum+now.size()<n)
            {
                sum+=now.size();
            }
            else if(sum+now.size()==n) 
            {
                cout<<now[now.size()-1]<<endl;
                break;
            }
            else 
            {
                ll pos=n-sum;
                cout<<now[pos-1]<<endl;
                break;
            }
        }
    }
    return 0;
}

  

#include <stdio.h>
#define ll long long
int lg(ll n){
	int res = 1;
	n /= 10;
	while (n > 0){
		n /= 10;
		res++;
	}
	return res;
}
int putDigit(ll num, ll d, ll len){
	while (++d < len)
		num /= 10;
	return num % 10;
}
int main (void){
	ll n, rows = 0, digit = 1;
	scanf("%lld", &n);
	while(n > rows){
		n -= rows;
		rows += lg(digit++);
	}
	ll base = 9, ith = 1;
	digit = 1;
	while (n > base * digit){
		n -= base * digit;
		digit++;
		ith += base;
		base *= 10;
	}
	printf("%d", putDigit(ith + (n - 1) / digit, (n - 1) % digit, digit));
	return 0;
}

  

【小米OJ-找到第N个数字Ⅱ】

上一篇:Android WifiDisplay分析三:RTSP交互以及数据传输


下一篇:大厂面试真题题解:搜索二维矩阵