跟你沟通 @ 2023-02-08 19:12:41
代码如下
#include <iostream>
#include <string.h>
using namespace std;
int a[1003], b[1003], ans[1003];
int main() {
int n, t = 0, la = 1, lb = 0, lans = 0;
//ad用来存储进位的数
cin >> n;
a[0] = 1;
for (t = 1; t <= n; ++t) {
int x = 0, ad = 0;
do {//半高精乘法---a*=t
x = a[lb] * t + ad;
b[lb] += x % 10;
ad = x / 10;
lb++;
} while (lb<=la);//使用do...while是因为之前的代码有问题,后来没改,应该不影响
ad = 0;//进位归0
for (int i = 0; i <= lb; ++i) {//高精度加法将当前的阶乘累加到答案ans数组
ans[i] += b[i] + ad;
ad = ans[i] / 10;
ans[i] %= 10;
}
for (int i = 0; i < lb; ++i)
a[i] = b[i];//将b(当前的阶乘)复制到a以备下次循环
la = lb;//a的长度也复制过去(可能不是实际长度,会更长)
lb = 0;
memset(b, 0, sizeof(b));//b和b的长度归0
}
lans = la;
while (!ans[lans])
lans--;//调整输出的范围
for (int i = lans; i >= 0; --i)
cout << ans[i];
return 0;
}
by LaoXu666 @ 2023-02-08 20:34:16
你会Python吗
by LaoXu666 @ 2023-02-08 20:35:15
代码(Python)
import math
n = input()
n = int(n)
Ans = 0
for i in range(0, n):
Ans = Ans + math.factorial(i + 1)
print(Ans)
by 跟你沟通 @ 2023-02-08 21:14:41
@ITKing 是来练习c的