刚刚写出来(要用标准c++编译器)
//求一个大数的阶乘
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <ctime>
using namespace std;
int zhi(int a,size_t i)
//判断一个整数的第i位的值,从后往前。
{
size_t m=size_t( log10(double(a)) +1);
//a的位数
if(i>m || i==0) return 0;
for(size_t j=1;j<i;j++)
a=a/10;
return a%10;
}
void fact(int n)
{
clock_t t=clock();
//计算所花费的时间。
bool not_plus=1;
double m=0;
for(double i=1;i<=n;i++)
m+=log10(i);
size_t s=size_t(m+1),templen=1;
vector<int> c(s),ctemp(s); c[s-1]=1;
for(int i=1;i<=n;i++)
//1*2*3.....*99*100
{
size_t clen=templen,
nwide=size_t(log10(double(i))+1);
//判断i的位数
for(size_t j=1; j<= nwide; j++)
//j代表i的某位
{
int x=zhi(i,j),
ckv=0,
ckj=0,
cky=0,
ctkv=0, ctkj=0, ctky=0;
for(size_t k=1;k<=clen;k++)
{
ckv=x*c[s-k]+ckj;
//x与c每位相乘再加上进位的值。
cky=ckv%10;
ckj=ckv/10;
ctkv=ctemp[s-k-j+1]+cky+ctkj;
ctky=ctkv%10;
ctkj=ctkv/10;
ctemp[s-k-j+1] = ctky;
}
if(j>1 && not_plus) templen++;
if(ckj>0 || ctkj>0)
{
ctemp[s-clen-j]+=ckj+ctkj;
//
templen++;
not_plus=0;
}
else
not_plus=1;
}
c.swap(ctemp);
ctemp.assign(s,0);
}
ofstream fout("jiecheng.txt");
if(!fout.is_open())
{
cout<<"can't open file jiecheng.txt"<<endl;
}
cout<<"\n\n所计算"<<n<<"的阶乘如下(已存为文件):\n\n";
fout<<"\n\n所计算"<<n<<"的阶乘如下:\n\n";
fout<<"结果的位数为:m=="<<int(m+1)<<endl<<endl;
cout<<"结果的位数为:m=="<<int(m+1)<<endl<<endl;
for(size_t i=0;i<s;i++)
{
if(i%5==0) fout<<ends<<ends;
if(i%30==0) fout<<endl;
fout<<c[i]<<ends;
}
fout<<endl<<endl;
for(size_t i=0;i<s;i++)
{
if(i%5==0) cout<<ends<<ends;
if(i%30==0) cout<<endl;
cout<<c[i]<<ends;
}
cout<<endl<<endl;
t=clock()-t;
fout<<"计算所花费的时间为:"<<t/1000<<"
秒.\n\n\n";
cout<<"计算所花费的时间为:"<<t/1000<<"
秒.\n\n\n";
fout.close();
}
int main()
{
cout<<"阶乘计算器....\n\n";
int a;
cout<<"Please input a number:\n
a==";
cin>>a;
fact(a);
cin>>a;
}