# 回《笔试常见的“阶乘”编程题，你写对了么？》

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include
<stdio.h>
#include
<string>
#include
<iomanip>
#include
<algorithm>
usingnamespacestd;
#include
"time.h"

constintMAX_GROUPS=20000;//最多2万组，每组最多4位整数，即最多可容纳8万位整数
constintMAXN=9999;//每组的上限值
constintGROUP_LEN=4;//每组的最大长度

classBigInteger
{
private:
intdata[MAX_GROUPS];
intlen;
voidinit()
{
memset(data,
0,sizeof(data));
}
public:
BigInteger()
{
init();
len
=0;
}
BigInteger(
constintb);
BigInteger(
constBigInteger&);

booloperator>(constBigInteger&)const;
BigInteger
&operator=(constBigInteger&);
BigInteger
BigInteger
&sub(constBigInteger&);
BigInteger
operator+(constBigInteger&)const;
BigInteger
operator-(constBigInteger&)const;
BigInteger
operator*(constBigInteger&)const;
BigInteger
operator/(constint&)const;
voidprint();
};
BigInteger::BigInteger(
constintnum)
{
intres,tmp=num;
len
=0;
init();
while(tmp>MAXN)
{
res
=tmp-tmp/(MAXN+1)*(MAXN+1);
tmp
=tmp/(MAXN+1);
data[len
++]=res;
}
data[len
++]=tmp;
}
BigInteger::BigInteger(
constBigInteger&rhs):len(rhs.len)
{
inti;
init();
for(i=0;i<len;i++)
{
data[i]
=rhs.data[i];
}
}
boolBigInteger::operator>(constBigInteger&rhs)const
{
intln;
if(len>rhs.len)
{
returntrue;
}
elseif(len<rhs.len)
{
returnfalse;
}
elseif(len==rhs.len)
{
ln
=len-1;
while(data[ln]==rhs.data[ln]&&ln>=0)
{
ln
--;
}
if(ln>=0&&data[ln]>rhs.data[ln])
{
returntrue;
}
else
{
returnfalse;
}
}

}

BigInteger
&BigInteger::operator=(constBigInteger&rhs)
{
init();
len
=rhs.len;
for(inti=0;i<len;i++)
{
data[i]
=rhs.data[i];
}
return*this;
}
BigInteger
{
inti,nLen;

nLen
=rhs.len>len?rhs.len:len;
for(i=0;i<nLen;i++)
{
data[i]
=data[i]+rhs.data[i];
if(data[i]>MAXN)
{
data[i
+1]++;
data[i]
=data[i]-MAXN-1;
}
}
if(data[nLen]!=0)
{
len
=nLen+1;
}
else
{
len
=nLen;
}

return*this;
}
BigInteger
&BigInteger::sub(constBigInteger&rhs)
{
inti,j,nLen;
if(len>rhs.len)
{
for(i=0;i<nLen;i++)
{
if(data[i]<rhs.data[i])
{
j
=i+1;
while(data[j]==0)j++;
data[j]
--;
--j;
while(j>i)
{
data[j]
+=MAXN;
--j;
}
data[i]
=data[i]+MAXN+1-rhs.data[i];
}
else
{
data[i]
-=rhs.data[i];
}
}
len
=nLen;
while(data[len-1]==0&&len>1)
{
--len;
}
}
elseif(len==rhs.len)
{
for(i=0;i<len;i++)
{
data[i]
-=rhs.data[i];
}
while(data[len-1]==0&&len>1)
{
--len;
}
}
return*this;
}
BigIntegerBigInteger::
operator+(constBigInteger&n)const
{
BigIntegera
=*this;
returna;
}
BigIntegerBigInteger::
operator-(constBigInteger&T)const
{
BigIntegerb
=*this;
b.sub(T);
returnb;
}
BigIntegerBigInteger::
operator*(constBigInteger&rhs)const
{
BigIntegerresult;
inti,j,up;
inttemp,temp1;

for(i=0;i<len;i++)
{
up
=0;
for(j=0;j<rhs.len;j++)
{
temp
=data[i]*rhs.data[j]+result.data[i+j]+up;
if(temp>MAXN)
{
temp1
=temp-temp/(MAXN+1)*(MAXN+1);
up
=temp/(MAXN+1);
result.data[i
+j]=temp1;
}
else
{
up
=0;
result.data[i
+j]=temp;
}
}
if(up!=0)
{
result.data[i
+j]=up;
}
}
result.len
=i+j;
while(result.data[result.len-1]==0&&result.len>1)result.len--;
returnresult;
}
BigIntegerBigInteger::
operator/(constint&b)const
{
BigIntegerret;
inti,down=0;

for(i=len-1;i>=0;i--)
{
ret.data[i]
=(data[i]+down*(MAXN+1))/b;
down
=data[i]+down*(MAXN+1)-ret.data[i]*b;
}
ret.len
=len;
while(ret.data[ret.len-1]==0)ret.len--;
returnret;
}
voidBigInteger::print()
{
inti;

cout
<<data[len-1];
for(i=len-2;i>=0;i--)
{
cout.width(GROUP_LEN);
cout.fill(
'0');
cout
<<data[i];
}
cout
<<endl;
}
intmain()
{
clock_tstart,finish;
doubleduration;
inti,n;
BigIntegerresult,num;
scanf(
"%d",&n);
start
=clock();
result
=BigInteger(1);
for(i=2;i<=n;++i)
{
num
=BigInteger(i);
result
=result*num;
}
result.print();
finish
=clock();
duration
=(double)(finish-start)/CLOCKS_PER_SEC;
printf(
"%fseconds/n",duration);
return0;
}

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->我的机器配置：酷睿双核2.0G,内存3G,

.312秒，在HDU的服务器上，10000的阶乘用时不到900毫秒。

C/C++确实是会快些的，HDU的这道题目对Java程序的时限是5秒内完成1万的阶乘，但c/c++的时限是1秒，由此可以看出。