Pagini recente » Cod sursa (job #176878) | Cod sursa (job #2974853) | Cod sursa (job #1511698) | Cod sursa (job #2929232) | Cod sursa (job #1817796)
#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 = 400000015;// 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;
}