Pagini recente » Monitorul de evaluare | Cod sursa (job #538316)
Cod sursa(job #538316)
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#define DEBUG 0
char *path1 = "C:\\Users\\madi\\workspace\\arena\\data.in";
char *path2 = "C:\\Users\\madi\\workspace\\arena\\data.out";
void write_out(int aux) {
FILE* f;
if (DEBUG)
f = fopen(path2, "wt");
else
f = fopen("fact.out", "wt");
fprintf(f, "%d\n",aux);
fclose(f);
exit(0);
}
int main() {
int n, p,c, aux, i, sum, max, pow, err;
//printf(strcat(path, ".in\n"));
FILE*f;
if (DEBUG)
f = fopen(path1, "rt");
else
f = fopen("fact.in", "rt");
int * pows;
int * zeros;
fscanf(f, "%d", &p);
fclose(f);
max = p;
n = 0;
if (p == 0)
write_out(1);
n = p * 5;
pow = 0;
while (n/5 > 0) {
n = n / 5;
pow++;
//printf("%d %d \n", n, pow);
}
pows = (int*) malloc ((pow + 2)*sizeof(int));
zeros = (int*) malloc ((pow + 2)*sizeof(int));
pows[1] = 5;
zeros[1] = 1;
aux = 5;
for (i = 2; i <= pow + 1; i++) {
aux = aux * 5;
pows[i] = aux;
zeros[i] = 5 * zeros[i-1] + 1;
//printf("%d %d %d\n", i, pows[i], zeros[i]);
}
aux = 0; i = pow; n = 0; err = 0;
while (n < p) {
sum = 0;
while (n + zeros[i] <= p) {
aux += pows[i];
n += zeros[i];
sum++;
//printf("%d %d %d %d %d\n",i, pows[i], zeros[i], aux, n);
}
i--;
//printf("%d\n", sum);
if (sum == 5)
err = 1;
}
//printf("\n%d %d\n", aux, n);
if (err == 0)
write_out(aux);
else
write_out(-1);
return 0;
}