Cod sursa(job #1259223)

Utilizator pitbull007Hurmuzache Ciprian pitbull007 Data 9 noiembrie 2014 20:37:47
Problema Factorial Scor 90
Compilator c Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <stdio.h>
#include <stdlib.h>


/**
    Function which returns the number of leading zeros of n!
**/
int nrZeros(int n) {
    int five=5;
    int counter=0;

    while(n/five > 0) {
        counter = counter+(n/five);
        five=five*5;
    }

    return counter;
}

int main(void) {

    FILE *fin,*fout;

    fin=fopen("fact.in","r");
    fout=fopen("fact.out","w");

    int p,i;
    fscanf(fin,"%d",&p);

    if(p == 0) {
        fprintf(fout,"1");
    }else {
        int ls = 0;
        int ld = 5*p+1;
        int m = (ld+ls)/2;
        int ok=0;
        int found=0;
        while(ls <= ld && !ok) {
            if(nrZeros(m) == p) {
                ok=1;
                found=m;
            }else {
                int zeros = nrZeros(m);
                if(zeros < p)
                    ls=m+1;
                else
                    ld=m;
            }
            m=(ld+ls)/2;
        }
        if(ok == 1){
            fprintf(fout,"%d",found-(found%5));
        }
        else{
            fprintf(fout,"-1");
        }
    }

    return 0;
}