Cod sursa(job #1253864)

Utilizator costty94Duica Costinel costty94 Data 1 noiembrie 2014 21:50:35
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int cifra(int n)
{
	int s, p = 5, i;
	s = 0;
	while(p <= n)
	{
		s += n / p;
		p = p * 5;
	}
	return s;
}
int n, st, dr, mij, ok;

int main()
{
	freopen("fact.in", "r", stdin);
	freopen("fact.out", "w", stdout);

	scanf("%d", &n);
	st = 1;
	dr = 1000000000;
	ok = 0;
	if(n == 0)
	{
		printf("1");
		return 0;
	}
	while(st <= dr && !ok)
	{
		mij = (st + dr) / 2;
		if(cifra(mij) > n)
			dr = mij - 1;
		else if(cifra(mij) < n)
			st = mij + 1;
		else
		{
			ok = 1;
			
		}
	}
	while(mij >= 0 && mij % 5 != 0)
		mij--;
	if(mij != 0)
		printf("%d", mij);
	else
		printf("-1");
	return 0;

}