原题链接:https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/
int countDigitOne(int n) {
long digit = 1, res = 0;
int high = n / 10, cur = n % 10, low = 0;
while (high != 0 || cur != 0) {
if (cur == 0) {//当cur=0时,1的出现次数只由high决定
res += high * digit;
} else if (cur == 1) {//当cur=1时,1的出现次数由高位high和地位low决定,计算公式为high*digit + low + 1
res += high * digit + low + 1;
} else {//当cur = 2,3,...,9时,此时1的出现次数只由high决定,计算公式为(high + 1) * high
res += (high + 1) * digit;
}
low += cur * digit;
cur = high % 10;
high /= 10;
digit *= 10;
}
return res;
}