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(); } }