求助,思路到这里不知道怎么进行了

P1009 [NOIP1998 普及组] 阶乘之和

cloudins @ 2023-10-05 21:25:43


#include <bits/stdc++.h>

using namespace std;

int a[10000],b[10000],c[10000],la,lb,lc;

int fi(int x){//阶乘
    if (x == 1){
        return 1;
    } else{
        return x* fi(x-1);
    }
}

 void cheng(string x,string y){//高精度加法
    la = x.length();
    lb = y.length();
    //反转数组
    for (int i = 0; i < la; ++i) {
        a[la - i] = x[i]-'0';
    }
    for (int i = 0; i < lb; ++i) {
        b[lb - i] = y[i]-'0';
    }
    //竖式计算
    lc = max(la,lb);
    for (int i = 0; i < lc; ++i) {
        c[i] += a[i] + b[i];
        c[i+1] = c[i] / 10;
        c[i] %= 10;
    }
    //输出
    if (c[lc+1] > 0) lc++;

    for (int i = lc; i >= 1; ++i) {
        cout << c[i];
    }
}

int main(){
    /*
     * 阶乘
     * 高精度
     */
    int a,sum=0;
    cin >> a;
    for (int i = 1; i <= a; ++i) {
        cheng(sum, fi(i));//这里思路开始乱了
    }
    cout << sum;
    return 0;
}

by guoguo02 @ 2023-10-11 21:58:21

你可以直接打表的


|