Cod sursa(job #538296)

Utilizator jessica26Ileana Stuparu jessica26 Data 21 februarie 2011 01:15:03
Problema Factorial Scor 90
Compilator c Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <time.h>

void write_out(int aux) {
	FILE* f;
	//f = fopen(path2, "wt");
	f = fopen("fact.out", "wt");
	fprintf(f, "%d\n",aux);
	fclose(f);
	exit(0);
}
int main() {

	int n, p,c, aux, i, sum, max, pow;
	char *path1 = "C:\\Users\\madi\\workspace\\arena\\data.in";
	char *path2 = "C:\\Users\\madi\\workspace\\arena\\data.out";
	//printf(strcat(path, ".in\n"));
	//FILE*f = fopen(path1, "rt");
	FILE*f = fopen("fact.in", "rt");
	int * pows;
	int * zeros;

	fscanf(f, "%d", &p);
	fclose(f);

	max = p;
	n = 0;
	if (p == 0)
		write_out(1);

	n = p * 5;
	pow = 0;
	while (n/5 > 0) {
		n = n / 5;
		pow++;
		//printf("%d %d \n", n, pow);
	}

	pows = (int*) malloc ((pow + 2)*sizeof(int));
	zeros = (int*) malloc ((pow + 2)*sizeof(int));
	pows[1] = 5;
	zeros[1] = 1;
	aux = 5;
	for (i = 2; i <= pow + 1; i++) {
		aux = aux * 5;
		pows[i] = aux;
		zeros[i] = 5 * zeros[i-1] + 1;
		//printf("%d %d %d\n", i, pows[i], zeros[i]);
	}
	aux = 0; i = pow; n = 0;
	while (n < p) {
		while (n + zeros[i] <= p) {
			aux += pows[i];
			n += zeros[i];
			//printf("%d %d %d %d %d\n",i, pows[i], zeros[i], aux, n);

		}
		i--;
	}
	//printf("\n%d\n", aux);
	write_out(aux);

return 0;
}