高精度阶乘,超时求优化
http://acm.
#include <iostream>
#define maxlen 100000
using namespace std;
struct HP
{
int len,s[maxlen];
};
void PrintHP(HP x)
{
for (int i=x.len;i>=1;i--)
cout<<x.s[i];
}
void Int2HP(int a,HP &x)
{
if (a==0)
{
x.len=1;x.s[1]=0;return;
}
for (x.len=0;a>0;)
{
x.s[++x.len]=a%10;
a/=10;
}
}
void Multi(const HP a,const HP b,HP &c)
{
int i,j;
c.len=a.len+b.len;
for (i=1;i<=c.len;i++) c.s[i]=0;
for (i=1;i<=a.len;i++)
for (j=1;j<=b.len;j++)
c.s[i+j-1]+=a.s[i]*b.s[j];
for (i=1;i<c.len;i++)
{
c.s[i+1]+=c.s[i]/10;
c.s[i]%=10;
}
while (c.s[i])
{
c.s[i+1]=c.s[i]/10;
c.s[i]%=10;
i++;
}
while (i>1 && !c.s[i])
{
i--;
c.len=i;
}
}
int main()
{
HP ans,temp,inte;
int a,i;
while (cin>>a )
{
if (a)
{
temp.s[1]=1;
temp.len=1;
for (i=1;i<=a;i++)
{
Int2HP(i,inte);
Multi(temp,inte,ans);
temp=ans;
}
PrintHP(ans);
cout<<endl;
}
}
}
[ 本帖最后由 love24114 于 2012-1-24 11:34 编辑 ]