求解特定数字的个数问题
求解特定数字的个数问题
求解从1到n的所有数中,特定数字m(0 <= m <= 9)的个数问题
uint64_t countN(uint64_t x, uint64_t n)
{
uint64_t lowernum;
uint64_t currentnum;
uint64_t highernum;
uint64_t factor = 1;
uint64_t ret = 0;
while (0 < x / factor) {
highernum = x / (10 * factor);
currentnum = x / factor % 10;
lowernum = n - (n / factor * factor);
if (currentnum < n) {
ret += highernum * factor;
} else if (currentnum == n) {
ret += highernum * factor + lowernum + 1;
} else {
ret += (highernum + 1) * factor;
}
factor *= 10;
}
return ret;
}