2012年10月20日

UVa 12241 - Digits Count


#include <stdio.h>

void cal(long long n, long long* num) {
  long long fac = 1;
  while (n / fac) {
    long long low = n - (n / fac) * fac, now = (n / fac) % 10, high = n / (fac * 10);
    int i;
    for (i = 0; i < 10; i++) {
      if (now < i) num[i] += high * fac;
      else if (now == i) num[i] += high * fac + low + 1;
      else if (now > i) num[i] += (high + 1) * fac;
    }
    fac *= 10;
  }
  while ((fac /= 10))
    num[0] -= fac;
}

int main() {
  int start, end;
  while (scanf("%d%d", &start, &end) && start || end) {
    long long ans[10] = {}, sub[10] = {};
    cal(start - 1, sub);
    cal(end, ans);
    int i;
    for (i = 0; i < 10; i++)
      printf("%lld%c", ans[i] - sub[i], i == 9 ? '\n' : ' ');
  }
  return 0;
}

沒有留言:

張貼留言