新手50分求教!!!

P1009 [NOIP1998 普及组] 阶乘之和

Vento_0410 @ 2025-01-09 22:46:37

#include <stdio.h>
#include <string.h>
int fac(long m)
{
    long j;
    if(m<0) {printf("输入错误"); return 1;}
    else if(m==0||m==1) j=1;
    else j=fac(m-1)*m;
    return (j); 
}
int main ()
{
  long n=0,S=0,s=0,m;
  scanf("%ld",&n);
  for(m=1;m<=n;m++)
  {
    s=fac(m);
    S+=s;
  }
  printf("%ld",S);
    return 0;
}

by Gilbert1206 @ 2025-01-09 22:48:26

高精!


by csxx601cjy @ 2025-01-09 22:49:17

@wudishidaifen

需要用高精度,否则会炸。

import math;
print(sum([math.factorial(i) for i in range(1,int(input())+1)]))

by FuZhenghao @ 2025-01-09 23:07:38

需要用到高精度加法和高精乘低精(不想写高精可以用python


|