c++求助,为啥后面两个过不去

P1009 [NOIP1998 普及组] 阶乘之和

lt1526679338 @ 2023-03-18 15:13:16

#include<iostream>
using namespace std;
int main(){
    int n,sum=0,sum1;
    cin>>n;
    for(int i=1;i<=n;i++){
        sum1=1;
        for(int j=1;j<=i;j++){
            sum1=sum1*j;
        }
        sum=sum+sum1;
    }
    cout<<sum<<endl;
    return 0;
} 

by Xiehongsheng @ 2023-03-23 20:52:24

用int怎么行呢

要用高精度来做呀(long long。。。)


by fengshuoEddie @ 2023-03-30 21:30:05

@Xiehongsheng 你管long long叫高精度?


by Xiehongsheng @ 2023-04-01 13:30:25

不不不


by xxc123 @ 2023-04-10 22:19:37

@lt1526679338 求关注


#include<iostream>
using namespace std;
int a[1001],b[1001];
void add()
{
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        b[i]=b[i]+a[i]+t;
        t=b[i]/10;
        b[i]%=10;
    }
}
void multiplication(int x)
{
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        a[i]=a[i]*x+t;
        t=a[i]/10;
        a[i]%=10;
    }
}
void print()
{
    int flag=0;
    for(int i=1;i<=1000;i++)
    {
        if(b[i]!=0) flag=1;
        if(flag) cout<<b[i];
    }
}
int main()
{
    a[1000]=b[1000]=1;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        multiplication(i);
        add();
    }
    print();
    return 0;
}

by SCP5543 @ 2023-05-14 09:19:32

@xxc123 你牛逼


|