#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> mul(vector<int> A, vector<int> B) //高精度乘法模板
{
while (B.size() >= 1 && B.back() == 0) B.pop_back();
vector<int> C(A.size() + B.size() + 7, 0);
for (int i = 0; i < A.size(); i ++) {
for (int j = 0; j < B.size(); j ++) {
C[i + j] += A[i] * B[j];
}
}
for (int i = 0; i + 1 < C.size(); i ++) {
C[i + 1] += C[i] / 10;
C[i] %= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
vector<int> add(vector<int> A, vector<int> B) //高精度加法模板
{
if (A.size() < B.size()) swap(A, B);
vector<int> C;
int t = 0;
for(int i = 0; i < A.size(); i ++)
{
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t%10);
t /= 10;
}
if (t) C.push_back(t);
return C;
}
int main() {
int n;
vector<int> b;
cin >> n;
for(int i = 1; i <= n; i ++) //循环,每次循环进行加法一次
{
vector<int> C;
C.push_back(1);//对C重新赋值进行后续计算
for(int j = 1; j <= i; j ++) //计算乘阶
{
vector<int> c;
if(j >= 10) //if其实可以省略,为了方便各位理解我加上了if
{
c.push_back(j%10);
c.push_back(j/10);
}
else c.push_back(j%10);
C = mul(C, c);
//cout << C[0] << "****";
}
b = add(C, b);
//cout << b[0] << " " << "||" << endl;
}
for(int i = b.size() - 1; i >= 0; i --) cout << b[i]; //完美输出
return 0;
}
先上代码,关于这道题就是考察高精度加法乘法的结合运用,注意考虑进位问题即可