P1009 [NOIP1998 普及组] 阶乘之和(高精度)

# [NOIP1998 普及组] 阶乘之和

## 题目描述

用高精度计算出 $S = 1! + 2! + 3! + \cdots + n!$($n \le 50$)。

其中 `!` 表示阶乘,定义为 $n!=n\times (n-1)\times (n-2)\times \cdots \times 1$。例如,$5! = 5 \times 4 \times 3 \times 2 \times 1=120$。

## 输入格式

一个正整数 $n$。

## 输出格式

一个正整数 $S$,表示计算结果。

## 样例 #1

### 样例输入 #1

```
3
```

### 样例输出 #1

```
9
```

## 提示

**【数据范围】**

对于 $100 \%$ 的数据,$1 \le n \le 50$。

#include <iostream>
using namespace std;
int a[150];
int b[150];
void cheng(int i,int *a)
{
    int w=0;
    for(int j=1;j<=150;++j)
    {
        a[j]=a[j]*i+w;
        w=a[j]/10;
        a[j]=a[j]%10;
    }
}
void he(int *a,int *b)
{
    int w=0;
    for(int y=1;y<=150;++y)
    {
        b[y]=b[y]+a[y]+w;
        w=b[y]/10;
        b[y]=b[y]%10;
    }
}
int main()
{
    int n;
    cin>>n;
    a[1]=1;
    for(int i=1;i<=n;++i)
    {
        cheng(i,a);
        he(a,b);
    }
    bool flag=0;
    for(int x=150;x>=1;--x)
    {
        if(b[x]!=0)
        {
            flag=1;
        }
        if(flag)
        {
            cout<<b[x];
        }
    }
    return 0;
}
posted @ 2022-07-25 22:47  四面楚歌2022  阅读(188)  评论(0)    收藏  举报