Cod sursa(job #788448)

Utilizator dogDaysAreOverAndreea Gheorghe dogDaysAreOver Data 14 septembrie 2012 23:33:23
Problema Factorial Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
#include <math.h>

#define inputFile "fact.in"
#define outputFile "fact.out"
#define MAX 16000

using namespace std;

int p, n;

int main(){
	ifstream in(inputFile);
	in>>p ;

	int countDigits = 0;
	n = 1;
	int countEven = 0;
	int countFive = 0;
	int tmp ;

	while(countDigits < p){
		n++;
		tmp = n;

		if(n%10 == 0){
			countDigits++;
			tmp = n/10;
		}

		while(tmp%5 ==0 ){
			countFive ++;
			tmp = tmp / 5;
		}


		while(tmp%2 == 0){
			countEven ++;
			tmp = tmp/2;
		}

		int moreD = min(countEven , countFive);

		if( moreD != 0){
			countDigits += moreD;
			countEven -= moreD;
			countFive -= moreD;
		}
	}


	ofstream output;
	output.open(outputFile);

	output << n << endl;
	output.close();
}