Cod sursa(job #2614241)

Utilizator georgeblanarBlanar George georgeblanar Data 11 mai 2020 15:07:15
Problema Factorial Scor 85
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
std::ifstream f("fact.in");
std::ofstream g("fact.out");

int findTrailingZeros(int n)
{
    int count = 0;
    for (int i = 5; n / i >= 1; i *= 5)
        count += n / i;

    return count;
}

int main()
{
	int p;
	f >> p;
	f.close();
	if (p == 0)
	{
		g << "1";
		return 0;
	}
	
	int n = 5 * p;
    int nb_of5 = findTrailingZeros(n);
	int temp, count_temp = 0;

	do
	{
		temp = n;
		count_temp = 0;
		
		do
		{
			temp /= 5;
			count_temp++;

		} while (temp % 5 == 0);
		nb_of5 -= count_temp;
		n -= 5;
	} while (nb_of5 >= p);

	if (nb_of5 + count_temp > p)
	{
		g << "-1";
	}
	else
	{
		g << n + 5;
		
	}
	g.close();
	return 0;
}