50分求助

P1009 [NOIP1998 普及组] 阶乘之和

kjll @ 2023-10-07 20:33:30

#include<stdio.h>
int main()
{ int n,k,i;
      k=1;
    double sum;
        sum=0;
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {k=k*i;
       sum=sum+k;}
       printf("%.lf",sum);
       return 0;
}

by Hatsunatsu @ 2023-10-07 20:34:59

@kjll 这题要写高精度


by Lizichen_licis @ 2023-10-11 19:43:33

高精度,你这要是python也能过。 C++要写高精度 @kjll


by _FastFT2013 @ 2023-10-14 13:09:46

@kjll 这是高精度


by flyWang @ 2023-10-14 13:57:06

@kjll 得写高精度,还有,这题是综合题,你得先会高精度加和高精度乘才能做,建议先做 1601 和 1303


by ouiostream @ 2023-10-18 13:01:29

#include<bits/stdc++.h>
using namespace std;
const int N=1001;
int a[N],b[N];
int n,len,x;
void multiplication(int num){
    x=0;
    int i;
    for(i=1;i<=len;i++){
        a[i]=a[i]*num+x;
        x=a[i]/10;
         a[i]%=10; 
    }
    while(x){
        a[i]=x%10;
        x/=10;
        i++;
    }
    len=i-1; 
}
void add(){
    x=0;
    for(int i=1;i<=len;i++){
        b[i]+=a[i]+x;
        x=b[i]/10;
        b[i]%=10;
    }
}
int main(){
    cin>>n;
    a[1]=1;
    len=1;
    for(int i=1;i<=n;i++){
        multiplication(i);
        add();
    }
    for(int i=len;i>=1;i--)
        cout<<b[i];
    cout<<endl; 
    return 0;
} 

by ouiostream @ 2023-10-18 13:02:11

得用高精度,下方代码仅供参考


|