P1009 [NOIP1998 普及组] 阶乘之和
题目链接
题目思路
本题为高精度乘法 + 高精度加法
题目代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1010;
int n;
int a[N], b[N];
int main()
{
cin >> n;
a[0] = b[0] = 1;
for(int i = 2; i <= n; i ++ )
{
for(int j = 0; j < 100; j ++ )
b[j] *= i;
for(int j = 0; j < 100; j ++ )
if(b[j] > 9)
{
b[j + 1] += b[j] / 10;
b[j] %= 10;
}
for(int j = 0; j < 100; j ++ )
{
a[j] += b[j];
if(a[j] > 9)
{
a[j + 1] += a[j] / 10;
a[j] %= 10;
}
}
}
int res;
for(res = 100; res >= 0 && a[res] == 0; res -- );
for(int j = res; j >= 0; j -- ) cout << a[j];
return 0;
}
孤独本是常态

浙公网安备 33010602011771号