Pagini recente » Cod sursa (job #683164) | Cod sursa (job #1018585) | Cod sursa (job #937442) | Cod sursa (job #252765) | Cod sursa (job #160351)
Cod sursa(job #160351)
#include <cstdio>
#include <cstdlib>
int globalVal = 0;
int getNear( int p )
{
if ( p == 0 )
return 1;
int nr = 1;
int val = 5;
while ( p > (nr * 5 + 1) )
{
nr = nr * 5 + 1;
val = val * 5;
}
if ( p <= nr * 4 )
{
int scale = p / nr;
globalVal += (val*scale);
return p-(scale*nr);
}
return -1;
}
int main ( int argc, char *argv[] )
{
FILE *fin = fopen( "factorial.in", "r" );
int p;
fscanf( fin, "%d", &p );
fclose( fin );
if ( p == 0 )
globalVal = 1;
while ( p > 0 )
p = getNear( p );
if ( p == -1 )
globalVal = -1;
FILE *fout = fopen( "factorial.out", "w" );
fprintf( fout, "%d\n", globalVal );
fclose( fout );
return 0;
}