UVa 1225 Digit Counting (枚举)

1225 - Digit Counting

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3666


N<10000,干脆O(NlogN)建表得了。


完整代码:

/*0.012s*/

#include<cstdio>

int c[10000][10];

int main()
{
	int i, k, t, n;
	for (i = 1; i < 10000; ++i)
	{
		for (k = i; k; k /= 10) ++c[i][k % 10];
		for (; k < 10; ++k) c[i][k] += c[i - 1][k];
	}
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		for (i = 0; i < 9; ++i)
			printf("%d ", c[n][i]);
		printf("%d\n", c[n][9]);
	}
	return 0;
}

你可能感兴趣的:(C++,ACM,uva)