你把这个运行一下,看看用时多少。完全是O(N^2)的。没
有任何优化
http://www.delphibbs.com/delphibbs/dispq.asp?lid=494104
10000! 要30s. 夸张
#include <stdio.h>
#include <time.h>
const int M = 10000;
const int N = 10000;
/*
N!
如果 N 再大点,就把M开大点
自己估计一下 N!有多少位就行了
*/
int mul(int a[],int n1,int b[],int n2,int c[]) {
int begin
c,inc;
int m,i,j;
m=n1+n2+3;
for( i=0;i<=m;i++) c=0;
for( i=1;i<=n2;i++) {
begin
c=i-1;
inc=0;
for( j=1;j<=n1;j++) {
begin
c++;
m=c[begin
c]+b*a[j]+inc;
inc=m/10000;
c[begin
c]=m-inc*10000;
}
if(inc>=1) c[begin
c+1]=inc;
}
if(inc>=1) {
begin
c++;
c[begin
c]=inc;
}
return begin
c;
}
void print(int a[],int len) {
int i;
if(!(len>1 &&
a[len]==0)) printf("%d",a[len]);
for(i=len-1;i>=1;i--) {
if(a>=1000) printf("%d",a);
else
if(a>=100) printf("0%d",a);
else
if(a>=10) printf("00%d",a);
else
if(a>=1) printf("000%d",a);
else
printf("0000");
}
printf("/n");
}
int main() {
freopen("output.out","w",stdout);
int p[2][M], con[2];
int l[2],i;
int pre,cur;
p[0][1]=l[0]=1;
pre = 0;
cur = 1;
for(i=2;i<=N;i++) {
con[1] = i;
l[cur] = mul(p[pre],l[pre],con,1,p[cur]);
pre = 1-pre;
cur = 1-cur;
}
print(p[pre],l[pre]);
double qq;
printf("use time = %.5lfs/n",(qq=clock())/CLK_TCK);
return 0;
}