51nod1057模拟乘法

输入N求N的阶乘的准确值。
Input输入N(1 <= N <= 10000)Output输出N的阶乘Sample Input
5
Sample Output
120

由于数量级很大,需采用模拟乘法的方法进位并存起来

#include<cstdio>

using namespace std;
#define Max 100000000        //切断位数
long long a[100000];
int main()
{
int n;
scanf("%d",&n);
int i,j,m = 0,c;                    //m储存次数
a[0] = 1;
for (i = 1;i <= n;i ++)
{
c = 0;
for (j = 0;j <= m;j ++)
{
a[j] = a[j] * i + c;
c = a[j] / Max;                         //c进位
a[j] %= Max;
}
if (c > 0)
{
a[++ m] =  c;
}
}
printf("%lld",a[m]);
for (i = m - 1;i >= 0;i --)
printf("%0.8lld",a[i]);
return 0;
}

猜你喜欢

转载自blog.csdn.net/cloudy_happy/article/details/79942091