Cod sursa(job #1207732)

Utilizator aurelian121bonte aurelian aurelian121 Data 13 iulie 2014 19:06:42
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
/*
 * app.cpp
 *
 *  Created on: Jul 12, 2014
 *      Author: Aurelian
 */
#include <iostream>
#include <stdio.h>
#include <fstream>

using namespace std;
int a[100], c[100];
void nrMari(long unsigned nr) {
	int i = 0;
	ofstream fout("fact.out");
	while (nr) {
		a[i] = nr % 10;
		nr /= 10;
		i++;
	}
	int ajutor = 0;
	int minte;
	int j;
	minte = 0;
	for (j = 0; j < i; j++) {

		c[j] = (c[j] + (a[j] * 5) % 10 + minte) % 10;
		minte = (a[j] * 5) / 10;

	}
	c[j] += minte;

	int l = i;
	if (c[l] == 0)
		l--;
	for (l; l >= 0; l--)
		fout << c[l];
}
int main() {

	long unsigned nr;
	ifstream fin("fact.in");
	fin >> nr;

	fin.close();
	ofstream fout("fact.out");
	if (nr == 0) {
		fout << 1;
		return 0;
	}
	if (nr < 0) {
		fout << -1;
		return 0;
	}
	long unsigned final = 0;
	long unsigned i;
	for (i = 1; i <= nr; i++) {
		final++;
		long unsigned j = i;
		while (j / 5 && j % 5 == 0) {
			i++;
			j = j / 5;
		}
	}
	if (i == nr + 1)
		nrMari(final);
	else
		fout << -1;
	return 0;
}