Cod sursa(job #2640250)

Utilizator bianca.ialangiBiancaI bianca.ialangi Data 5 august 2020 18:29:06
Problema Factorial Scor 10
Compilator c-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <stdio.h>

int main () {
	int p, pow, x, new_pow = 5, n;

	//scanf("%d", &p);

	pow = 25;
	x = 5 * p;

	FILE *in = fopen("fact.in", "r");
	FILE *out = fopen("fact.out", "wt");

	fscanf(in, "%d", &p);

	while (x >= pow) {
		pow = pow * 5;
	}

	pow = pow / 5;
	n = x / pow;

	if (pow > 5) {


		if (x == pow * n) {
			n--;
		}

		if (n == 0 && pow >5) {
			pow = pow / 5;
			n = x / pow;
			if (x == pow * n) {
				n--;
			}
		}

		x = 5 * (p - n);

		if (x / pow != n) {
			//fprintf(out,"-1\n");
			x = -1; 
		} 

		while (new_pow > pow/5) {
			if ((p - x / new_pow) > 0) {
				p = p - x / new_pow;
				new_pow = new_pow * 5;
			} else {
				x = -1;//else fprintf(out, "-1\n");
				new_pow = new_pow * 5;
			}
		}
	}



	fprintf(out, "%d\n", x);

	fclose(in);
	fclose(out);
}