Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Fractional Frequencies

324.c

#include <stdio.h>
#include <string.h>

typedef struct {
	int pos;
	int dig[1000];
} big;

int n;
int num[10];
big b;

void
bigmult(int m) {
	int i;
	int carry=0;

	for (i=0; i<b.pos; i++) {
		b.dig[i] *= m;
		b.dig[i] += carry;
		carry = b.dig[i] / 10;
		b.dig[i] %= 10;
	}
	while (carry) {
		b.dig[b.pos] *= m;
		b.dig[b.pos] += carry;
		carry = b.dig[b.pos] / 10;
		b.dig[b.pos] %= 10;
		b.pos++;
	}
}

void
fact(void) {
	int i;

	memset(&b, 0, sizeof(b));
	b.pos = 1;
	b.dig[0] = 1;

	for (i=2; i<=n; i++) {
		bigmult(i);
	}
}

void
count(void) {
	int i;
	memset(num, 0, sizeof(num));
	for (i=0; i<b.pos; i++) {
		num[b.dig[i]]++;
	}
}

void
print(void) {
	printf("%d! --\n", n);
	printf("   (%d) %4d    (%d) %4d    (%d) %4d    (%d) %4d    (%d) %4d\n",
			0, num[0], 1, num[1], 2, num[2], 3, num[3], 4, num[4]);
	printf("   (%d) %4d    (%d) %4d    (%d) %4d    (%d) %4d    (%d) %4d\n",
			5, num[5], 6, num[6], 7, num[7], 8, num[8], 9, num[9]);
}

int
main(void) {
	while (1) {
		scanf("%d", &n);
		if (!n) {
			return 0;
		}
		fact();
		count();
		print();
	}
}