Cod sursa(job #567331)

Utilizator XladhenianGrigorita Vlad-Stefan Xladhenian Data 29 martie 2011 22:27:02
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb

#include <iostream>
#include <fstream>
using namespace std;

long Levels[14];
long Level5[14];
long N;
long Fact;
long FactLevel;

int main(void)
{
 fstream f1("fact.in",ios::in);
 fstream f2("fact.out",ios::out);
 f1 >> N;
 if (N == 0)
   {
    f2 << 1;
    f1.close();
    f2.close();
    return 0;
   }
 Levels[0] = 1;
 Level5[0] = 5;
 for (int i = 1;i < 14;i = i + 1)
  {
   Levels[i] = Levels[i - 1] * 5 + 1;
   Level5[i] = Level5[i - 1] * 5;
  }
 Fact = 0;
 FactLevel = 13;
 while (N > 0)
  {
   if ((N - Levels[FactLevel]) == -1)
     {
      f2 << (-1);
      f1.close();
      f2.close();
      return 0;
     }
   while ((N - Levels[FactLevel]) >= 0)
    {
     N = N - Levels[FactLevel];
     Fact += Level5[FactLevel];
    }
   FactLevel--;
  }
 f2 << Fact;
 f1.close();
 f2.close();
 return 0;
}