标签:int position c++ find public printf etc dig osi
// language C with STL(C++)
// 剑指44
// https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/
// 同主站400题
// https://leetcode-cn.com/problems/nth-digit/
class Solution {
public:
long power_10(long i){
long res = 1;
while(i>0){
res*=10;
i--;
}
return res;
}
long sum(long i){
return ((9*i-1)*power_10(i+1)+10)/90;
}
int findNthDigit(int n){
if(n<1)
return n;
int i = 1;
while(n > sum(i))
i++;
i -=1;
// printf("%d-", i);
n -= sum(i);
// printf("%d-", n);
int position = n%(i+1); //正数第几个数位
// printf("%d", position);
n = power_10(i) + n/(i+1);
// printf("%d-", n); // 第几个数
if(position==0){
n-=1;
position = 1;
}
else
position = i+2-position;
n = n/power_10(position-1);
return n%10;
}
};
标签:int position c++ find public printf etc dig osi
原文地址:https://www.cnblogs.com/gallien/p/14379155.html