1. 程式人生 > >演算法設計與分析P8演算法實現第一題

演算法設計與分析P8演算法實現第一題

#include<stdio.h>
#include<string.h>
int main()
{
	int n, t, i,temp;
	int count[10];
	memset(count, 0, sizeof(count));
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
		t = i;
		while (t)
		{
			temp = t % 10;
			count[temp]++;
			t = t / 10;
		}
	}
	for (i = 0; i < 10; i++)
	{
		printf("%d\n", count[i]);
	}

	return 0;
}

第一種演算法遇到大數時會很慢