poj-2282

// 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, 以及最高位.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值