400. Nth Digit

 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     int findNthDigit(int n) 
12     {
13         long base=9,digits=1;
14         while(n-base*digits>0)
15         {
16             n-=base*digits;
17             base*=10;
18             digits++;
19         }
20         int index=n%digits;
21         if(index==0)
22             index=digits;
23         long num=1;
24         for(int i=1;i<digits;i++)
25             num*=10;
26         num+=(index==digits)? n/digits-1:n/digits;
27         for(int i=index;i<digits;i++)
28             num/=10;
29         return num%10;
30     }
31 };

先判定所在数字为几位数

再确定所在数字和索引位

最后构造出所在数字并输出结果

猜你喜欢

转载自www.cnblogs.com/zhuangbijingdeboke/p/9094033.html