Cod sursa(job #1329947)

Utilizator codrut_grosuGrosu Codrut-Cristian codrut_grosu Data 30 ianuarie 2015 01:45:52
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
# include <iostream>
# include <fstream>


int main () {

	std :: ifstream f ("fact.in");
	std :: ofstream g ("fact.out");

	int P;
	int nr = 0;
	int i;
	int a;
	int nr1 ;

	f >> P;	

	if ( P == 0 ) {
		g << 1;
	} else {
		a = 5;

		while (true) {
            i = a;
			nr += 5; 
            if ( i % 5 == 0 ) {
                while ( i % 5 == 0 ) {
                    i /=5;
                    nr ++;
                }
            }
 
            if ( nr == P ) {
                g << a*5;
                break;
            } else if ( nr > P ) {
            	nr1 = 0;
				i = a;
				while ( i % 5 == 0 ) {
					nr1 ++ ;
					i /= 5;
				}
				if ( nr - nr1 - 1 == P ) {
					g << ( a - 1) * 5;
					break;
				} else {
					nr -= nr1;
					a -= 1;
					nr --;
					while ( nr > P && a % 5 != 0 ) {
						a --;
						nr -- ;
					}
					if ( nr == P ) {
						g << a*5;
						break;
					} else {
						g << -1;
						break;
					}
				}
            }
            a += 5 ;
        }
    }
 
    g << '\n';
 
    return 0;
}