Cod sursa(job #2907383)

Utilizator agamanAlexandru Gaman agaman Data 30 mai 2022 00:09:00
Problema Factorial Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
// Infoarena.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include <fstream>
#include <math.h>

int factorial(int targetZeroes = 0) {
	if (targetZeroes == 0 || targetZeroes < 0) {
		return 1;
	}

	int n5 = 0;
	int n10 = 0;
	int index = 5;
	do {
		if (n10 + n5 > targetZeroes) {
			return -1;
		}

		int current = index;
		/*while (current % 10 == 0) {
			n10 += 1;
			current /= 10;
		}*/
		double l10 = log10(current);
		if (l10 == floor(l10)) {
			n10 += l10;
			current = l10;
		}

		double log5 = log2(current) / log2(5);
		if (log5 == floor(log5)) {
			n5 += log5;
		}
		else if (current % 5 == 0) {
			n5 += 1;
		}

		index += 5;
	} while (n10 + n5 != targetZeroes);

	return index - 5;
}

int main()
{
	//std::cout << "Executing..." << std::endl;

	std::ifstream in;
	in.open("fact.in");
	int target = 0;
	in >> target;
	in.close();

	//std::cout << "Computing factorial for " << target << " number of zeroes" << std::endl;
	int result = factorial(target);
	//std::cout << "Result is " << result << std::endl;

	std::ofstream out;
	out.open("fact.out");
	out << result;
	out.close();

	return 0;
	//std::cout << "Done!" << std::endl;
}