Cod sursa(job #1461132)

Utilizator cuna_christianCuna-Mic Mihai-Cristian cuna_christian Data 14 iulie 2015 20:19:35
Problema Factorial Scor 20
Compilator c Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int end_zero(int p){

    int zeroes = 0;

    while(p > 0){
        zeroes += p / 5;
        p /= 5;
    }

    return zeroes;
}

bool is_lower(int n, int p){
    --n;
    if(end_zero(n) == p)
        return true;
    else
        return false;
}

int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    int p, a = 0 , b = 1000000, c, ok = 0;

    scanf("%d", &p);

    while(a < b){
        c = (a+b) / 2;

        if(end_zero(c) == p){
            while(is_lower(c, p))
                --c;
            printf("%d", c);
            ok = 1;
            break;
        }
        else {
            if(end_zero(c) < p)
                a = c + 1;
            else
                if (end_zero(c) > p)
                    b = c - 1;
        }
    }

    if(!ok)
        printf("-1");

    return 0;
}