阶乘
作者:互联网
高精原理详细精准介绍见:详情
#include<iostream>
using namespace std;
int a[3000] = { 1 }, b, c[3000]; //a[3000] i-1阶乘 //b i //c[3000] i阶乘
int f[3000]; //f[3000] 阶乘累加
int lena = 1, lenc, lenf, temp;
int flag, flag2;
int get_length(int a)
{
int cnt = 0;
while (a > 0)
{
a /= 10;
cnt++;
}
return cnt;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++) //i-1 阶乘已保存在 a 中
{
flag = 0;
flag2 = 0;
for (int j = 0; j < lena; j++)
{
c[j] = a[j] * i;
}
temp = get_length(i);
for (int j = 0; j < lena + temp; j++)
{
if (c[j] > 10)
{
c[j + 1] += c[j] / 10, c[j] %= 10;
if (j == (lena + temp - 2)) flag = 1;
}
}
if (flag == 1) lenc = lena + temp;
else lenc = lena + temp - 1;
for (int j = 0; j < lenc; j++)
{
f[j] += c[j];
}
for (int j = 0; j < lenc + 1; j++) //c 的长度一定大于 f //因为 c = i 个 i-1阶乘, f < i-1 个 i-1阶乘
{
if (f[j] >= 10)
{
f[j + 1] += f[j] / 10, f[j] %= 10;
if (j == lenc - 1) flag2 = 1;
}
}
if (flag2 == 1) lenf = lenc + 1;
else lenf = lenc;
for (int j = 0; j < lenc; j++)
{
a[j] = c[j]; ///准备计算 i+1 阶乘
}
lena = lenc;
}
for (int i = lenf - 1; i >= 0; i--)
{
cout << f[i];
}
printf("\n");
return 0;
}
标签:lenc,10,lena,int,3000,阶乘 来源: https://www.cnblogs.com/ouyang1037/p/15809209.html