NC24432. ∞
描述
输入描述
The input contains multiple test cases.
The first line is an integer T(1 <= T <= 100000), which represents the number of test cases.
Each of the next T lines contains an integer p(1 <= p <= 1e18), represents the position has been asked.
输出描述
For each test case, output the digit at position p in in one single line.
示例1
输入:
5 1 10 19 66 1000000000000000000
输出:
1 1 4 3 3
说明:
Python(2.7.3) 解法, 执行用时: 455ms, 内存消耗: 6372K, 提交时间: 2019-04-14 13:10:51
import math def nth(n): if n<=9:return n tmp_sum = 9 num = 9 num2 = 1 while n>=tmp_sum: if n==tmp_sum:return 9 num*=10 num2+=1 tmp_sum+=(num*num2) tmp_sum -= (num*num2) n = n-tmp_sum c1 = n/num2 c2 = n%num2 if c2==0:return (num/9+c1-1)%10 return int(str(num/9+c1)[c2-1]) n = int(raw_input().strip()) for i in range(n): x = int(raw_input().strip()) print nth(x)
C++14(g++5.4) 解法, 执行用时: 73ms, 内存消耗: 2588K, 提交时间: 2019-04-19 16:09:04
#include<iostream> #include<cstring> #include<string> #include<cstdio> using namespace std; int main(){ int gt; long long p,w,t; scanf("%d",>); while(gt--){ scanf("%lld",&p); w=1,t=9; while(p>t*w){ p-=t*w; t*=10; w++; } long long m=p%w; t=t/9; long long s; if(m==0){ s=t+p/w-1; m=w; } else { s=t+p/w; } char ss[100]; sprintf(ss,"%lld",s); printf("%c\n",ss[m-1]); } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 329ms, 内存消耗: 640K, 提交时间: 2020-02-26 22:53:59
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int T; scanf("%d",&T); while(T--) { ll p; cin>>p; ll len=1; ll num=9; while(p>len*num) { p-=len*num; len++; num*=10; } ll number=num/9+(p-1)/len; ll loc=len-(p-1)%len; while(--loc) number/=10; cout<<number%10<<endl; } return 0; }
Python3(3.5.2) 解法, 执行用时: 1658ms, 内存消耗: 3692K, 提交时间: 2019-04-14 16:45:18
n = int(input()) def find(n): _len = 1 cnt = 9 start = 1 while n > _len * cnt: n -= _len * cnt _len += 1 cnt *= 10 start *= 10 start += (n - 1) // _len return int(str(start)[(n - 1) % _len]) for i in range(n): print(find(int(input())), sep='', end='\n')