#include<iostream>
#include<string>
using namespace std;
int a[301], b[301], c[301], tmp[301];
string s1, s2;
int n;
void strtoint(string s, int des[])
{
for (int i = 0; i < s.size(); i++) {
des[i + 1] = s[i] - '0';
}
}
void printArray(int arr[], int len)
{
for (int i = 1; i <= len; i++) {
cout << arr[i];
}
}
void printArray_reverse(int arr[], int len)
{
for (int i = len; i >= 1; i--) {
cout << arr[i];
}
}
void split(int N, int A[])
{
int M = 10000;
//个位、十位、百位、千位、万位
for (int i = 5; i >= 1; i--) {
A[i] = N / M;
N = N % M;
M /= 10;
}
}
void ctob(int B[], int C[], int LC) {
for (int i = 1; i <= LC; i++) {
B[i] = C[i];
}
}
void c_init(int C[], int lc) {
// c数组初始化为全0 ------初始化结果数组c
for (int i = 0; i <= lc; i++) {
C[i] = 0;
}
c[1] = 1;
}
int main()
{
cin >> n;
int la = 5, lb = 5, lc = 1, lt = 0;
c_init(c, lc);
for (int i = n; i >= 1; i--) {
la = 5, lb = 5, lc = 1;
//c_init(c,lc);//初始化结果数组c
for (int N = i; N >= 1; N--) {
//得到lc的长度
string s_temp = std::to_string(N);
la = s_temp.size();
lb = lc;
lc = lb + la;
//把上一次的结果c赋值给b数组------得到因子b
ctob(b, c, lc);
// c数组初始化为全0 ------初始化结果数组c
for (int i = 0; i <= lc; i++) {
c[i] = 0;
}
// 把N拆分到数组a中 -----得到因子a
split(N, a);
//高精度乘法-------c=a*b
for (int i = 1; i <= la; i++) {
for (int j = 1; j <= lb; j++) {
c[i + j - 1] += a[i] * b[j];
c[i + j] += c[i + j - 1] / 10;//进位
c[i + j - 1] %= 10;//留10
}
}
//去除c的前导0
while (c[lc] == 0 && lc > 1) {
lc--;
}
}
// 高精度加法累加求和
lt = max(lt, lc) + 1;
for (int i = 1; i <= lt; i++) {
tmp[i] += c[i];
tmp[i + 1] += tmp[i] / 10; //进位
tmp[i] = tmp[i] % 10; //留10
}
while (tmp[lt] == 0 && lt > 1) {
lt--;
}
c_init(c, lc);//初始化结果数组c
}
printArray_reverse(tmp, lt);
return 0;
system("pause");
}
C++阶乘和
于 2023-05-15 10:15:06 首次发布