如果要近似的,可以这么做:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include <iostream>
using namespace std;
int main(int argc,char *argv[]){
int i;
double s;
for(s=i=1;i<101;s*=i++);
cout << "100! = " << s << endl;
return 0;
}
如果要精确的,就得麻烦点:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include <iostream>
#include "math.h"
using namespace std;
int main(int argc,char *argv[]){
int i,j,n,*p;
double x;
for(x=0.0,i=2;i<101;x+=log10(i++));
if((p=new int[x/=6,n=(int)x+(x!=(int)x)])==NULL){
cout << "Application memory failure...\n";
return 0;
}
for(i=0;i<n;p[i++]=0);
for(p[n-1]=1,i=2;i<101;i++){
for(j=0;p[j]==0;j++);
while(j<n)
p[j++]*=i;
for(j--;j>0;j--)
if(p[j]>999999)
p[j-1]+=p[j]/1000000,p[j]%=1000000;
}
cout << "100! = " << p[0];
cout.fill('0');
for(i=1;i<n;cout.width(6),cout << p[i++]);
delete []p;
cout << endl;
return 0;
}