算一算每一位对应会有多少个0,然后两个数相减即可
#include <iostream> #include <algorithm> #include <string.h> using namespace std; #define int long long int cul(int x) { if (x < 0) return 0; int res = 1, last = 0, base = 1; while (x) { int tmp = x % 10; x /= 10; if (tmp) res += x * base; else res += (x - 1) * base + 1 + last; last += tmp * base; base *= 10; } return res; } signed main() { int n, m; while (scanf("%lld%lld", &n, &m) != EOF) { if (n == -1 && m == -1) break; printf("%lld\n", cul(m) - cul(n - 1)); } }