Cod sursa(job #476211)

Utilizator a.stanciuStanciu Adrian a.stanciu Data 10 august 2010 11:42:10
Problema Factorial Scor 85
Compilator c Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <stdio.h>

int main()
{
	int p, n = 0;
	FILE *f;

	f = fopen("fact.in", "r");
	fscanf(f, "%d", &p);
	fclose(f);

	int fact[] = {5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125};
	int zero[] = {1, 6, 31, 156, 781, 3906, 19531, 97656, 488281, 2441406, 12207031, 61035156, 305175781};

	int i;
	for (i = 12; i >= 0; i--)
		if (p >= zero[i])
		{
			n += p / zero[i] * fact[i];
			p = p % zero[i];
		}

	for (i = 0; i <= 12; i++)
		if (fact[i] == n && p < zero[i])
			n = -1;

	f = fopen("fact.out", "w");
	fprintf(f, "%d\n", n);
	fclose(f);
	
	return 0;
}