洛谷-P1009 阶乘之和

洛谷-P1009 阶乘之和

原题链接:https://www.luogu.com.cn/problem/P1009


题目描述

用高精度计算出\(S=1!+2!+3!+…+n! (n≤50)\)

其中“!”表示阶乘,例如:\(5!=5 \times 4 \times 3 \times 2 \times 1\)

输入格式

一个正整数\(N\)

输出格式

一个正整数\(S\),表示计算结果。

输入输出样例

输入 #1

3

输出 #1

9

C++代码

#include <iostream>
using namespace std;

int a[2000], c[2000];

void pplus(int a[], int c[]) {
    int jw = 0;
    for (int i=1; i<=1000; ++i) {
        c[i] += a[i] + jw;
        jw = c[i] / 10;
        c[i] %= 10;
    }
}

void mmultip(int a[], int c) {
    int jw = 0;
    for (int i=1; i<=1000; ++i) {
        a[i] = a[i] * c + jw;
        jw = a[i] /10;
        a[i] %= 10;
    }
}

int main() {
    int n, i;
    cin >> n;
    a[1] = 1;
    for (i=1; i<=n; ++i) {
        mmultip(a, i);
        pplus(a, c);
    }
    for (i=1000; c[i]==0; --i);
    for (; i>=1; --i)
        cout << c[i];
    cout << endl;
    return 0;
}
posted @ 2020-08-01 09:42  yuzec  阅读(249)  评论(0编辑  收藏  举报