// 380K 32MS G++
#include <stdio.h>
#include <string.h>
#include <math.h>
long long appearTime1[10];
long long appearTime2[10];
void getAppearTime(int num, long long * appearArray) {
appearArray[0] = 1;
if (num == 0) {
return;
}
int divisor1= 1;
int divisor2= 10;
int rightLength = 0;
while(1) {
if (divisor1 > num) {
break;
}
int curVal = (num/divisor1)%10;
int rightPart = num%divisor1;
int leftPart = num/divisor2;
// printf("curVal is %d, rightPart is %d, leftPart is %d\n", curVal, rightPart, leftPart);
int begin = 0;
// reach the most left unit, 0 can not appear here
if (!leftPart) {
begin = 1;
}
if (curVal > 0) {
appearArray[0] += (leftPart)*pow(10, rightLength);
} else {//curVal == 0;
appearArray[curVal] += (leftPart-1)*pow(10, rightLength);
appearArray[curVal] += rightPart;
}
// for numbers < curVal
for (int i = 1; i < curVal; i++) {
appearArray[i] += (leftPart+1)*pow(10, rightLength);
}
// for number == curVal
if (leftPart > 0 && curVal > 0) {
appearArray[curVal] += (leftPart)*pow(10, rightLength);
}
if (rightPart > 0 && curVal > 0) {
appearArray[curVal] += rightPart;
}
for (int i = curVal + 1; i <= 9; i++) {
appearArray[i] += (leftPart)*pow(10, rightLength);
}
// for numbers > curVal
rightLength++;
divisor1 *= 10;
divisor2 *= 10;
}
}
int begin;
int end;
int main() {
while(scanf("%d %d", &begin, &end) != EOF) {
if (!begin && !end) {
return 0;
}
int max = begin > end ? begin: end;
int min = begin <= end ? begin: end;
memset(appearTime1, 0, sizeof(appearTime1));
memset(appearTime2, 0, sizeof(appearTime2));
getAppearTime(max + 1, appearTime1);
if (min > 0) {
getAppearTime(min, appearTime2);
}
// for (int i = 0; i <= 9; i++) {
// printf("%lld", appearTime1[i]);
// if (i == 9) {
// printf("\n");
// } else {
// printf(" ");
// }
// }
// for (int i = 0; i <= 9; i++) {
// printf("%lld", appearTime2[i]);
// if (i == 9) {
// printf("\n");
// } else {
// printf(" ");
// }
// }
for (int i = 0; i <= 9; i++) {
printf("%lld", appearTime1[i] - appearTime2[i]);
if (i == 9) {
printf("\n");
} else {
printf(" ");
}
}
}
}
按照http://www.cnblogs.com/pcoda/archive/2012/04/25/2470256.html的思想进行了试刀,
果然比我原来的思路要来的简洁, 不过还是会存在一些特殊情况,比如某一位是0, 以及最高位.