```cpp int countNumX2(int n, int x) { int high = n / 10; int cur = n % 10; int low = 0; int digit = 1; int res = 0; while (high != 0 || cur != 0) { if (cur < x) { res += high * digit; } else if (cur == x) { res += high * digit + low + 1; } else if (cur > x) { res += (high + 1) * digit; } low = cur * digit; cur = high % 10; high /= 10; digit *= 10; } return res; } ```