50分求助

P1009 [NOIP1998 普及组] 阶乘之和

Rmghcz @ 2023-08-13 16:09:49

include<bits/stdc++.h>

using namespace std;


int main()
{
    int n,j=1,c,sum=0;     
    cin>>n;
    int m=n;
    for(int i=1;i<=m;i++)
    {
        int a=i;
        for(int b=1;b<=a;b++)
            {
                j*=b;
            }
        c=j;
        sum+=c;
        j=1;
    }
    cout<<sum<<endl;
    return 0;
}

by tongzhenxuan @ 2023-08-13 16:25:00

PS:如果希望通过本题,请继续学习第八章高精度的知识。


by tongzhenxuan @ 2023-08-13 16:25:11

@Rmghcz


by PengDave @ 2023-08-13 16:51:35

这是我的代码,仅供参考

#include<iostream>
using namespace std;
void cheng(int a[],int b){
    int a1[100]={};
    for(int i=0;i<100;i++){
        a1[i]+=a[i]*b;
        a1[i+1]+=a1[i]/10%10;
        a1[i+2]+=a1[i]/100;
        a1[i]=a1[i]%10;
    }
    for(int i=0;i<100;i++){
        a[i]=a1[i];
    }
    return;
}
void jia(int s[],int s1[]){
    int s2[100]={};
    for(int i=0;i<99;i++){
        s2[i]+=s[i]+s1[i];
        s2[i+1]+=s2[i]/10;
        s2[i]=s2[i]%10;
    }
    for(int i=0;i<100;i++){
        s[i]=s2[i];
    }
}
int main(){
    int n,s[100]={},sum[100]={};
    s[0]=1;
    cin>>n;
    for(int j=n;j>0;j--){
        for(int k=0;k<100;k++){
            s[k]=0;
        }
        s[0]=1;
        for(int i=1;i<=j;i++){
            cheng(s,i);
        }
        jia(sum,s);
    }
    int len=100;
    while(sum[len-1]==0&&len>0){
        len--;
    }
    for(int i=len-1;i>=0;i--){
        cout<<sum[i];
    }
    return 0;
}

by PengDave @ 2023-08-13 16:52:33

具体原理需要学习高精度乘法和加法


by _SkyDream_ @ 2023-08-13 17:05:25

@Rmghcz

1.需要高精度

2.你的markdown怎么了


by Sky_Sea @ 2023-08-13 20:12:51

题目要用高精度计算的


by Rmghcz @ 2023-08-15 14:25:12

@tongzhenxuan 好的,谢谢


by Rmghcz @ 2023-08-15 14:26:11

@PengDave 好的,谢谢


by Rmghcz @ 2023-08-15 14:27:13

@SkyDream 打的时候没注意,谢谢啦


by Rmghcz @ 2023-08-15 14:27:55

@Sky_Sea 知道啦,谢谢


|