Pagini recente » Cod sursa (job #2894098) | Cod sursa (job #1139736) | Cod sursa (job #1684656) | Cod sursa (job #1889852) | Cod sursa (job #2561946)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("fact.in");
ofstream out("fact.out");
//int power(int base, int exp);
bool factorial(int n,int p);
int multiply(int x,int res[],int res_size);
int main() {
int p, n;
bool no_match=true;
in >> p;
//nrO = power(10, p);
n=1;
while (no_match){
n++;
no_match=factorial(n,p);
}
if (p == 0) { out << 1; }
else if (n > 1) { out << n; }
else out << -1;
return 0;
}
/*
int power( int base, int exp) {
int result = 1;
for (int c = 1;c <=exp;c++) {
result = result * base;
}
return result;
}
*/
int multiply(int x,int res[],int res_size){
int carry=0;
for(int i=0;i<res_size;i++){
int prod=res[i]*x+carry;
res[i]=prod%10;
carry=prod/10;
}
while(carry){
res[res_size]=carry%10;
carry=carry/10;
res_size++;
}
return res_size;
}
bool factorial(int n,int p) {
int res[1000];
res[0]=1;
int res_size=1;
for(int x=2;x<=n;x++){
res_size=multiply(x,res,res_size);
}
for(int i=0;i<p;i++){
if(res[i]!=0){return true;}
}
return false;
}