Cod sursa(job #1814738)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 24 noiembrie 2016 14:55:19
Problema Factorial Scor 60
Compilator c Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <stdio.h>

int FindZeros(int someNumber){
int zeros=0, i;

for(i=5; i<=someNumber; i*=5){
    zeros += someNumber / i;
}
return zeros;
}

int main(){

FILE *file1, *file2;
int min, max, target, guess; //binary search variables
int calc;

file1 = fopen("fact.in", "r");
file2 = fopen("fact.out", "w");

fscanf(file1, "%d", &target);

min = 1;
max = 100000000;// STEP 1.

if(!target)
    fprintf(file2, "%d", 1);
else
while(1){

    if(max<min){
        fprintf(file2, "%d", -1);// STEP 2. target is not here
        break;
    }
    guess = (max + min) / 2;// STEP 3. compute guess
    calc = FindZeros(guess);

    if(calc==target){// STEP 4. you found it!
        fprintf(file2, "%d", guess - guess%5);
        break;
    }
    if(calc<target){// STEP 5. too low
        min = guess + 1;
    }
    if(calc>target){// STEP 6. too high
        max = guess - 1;
    }
}
return 0;
}