求,看看递归写对了没

P1009 [NOIP1998 普及组] 阶乘之和

ffgg57344 @ 2024-11-17 19:33:34

#include<iostream>
using namespace std;
long sum=1;
long digui(long i)
{
    if(i>=1)
    {
        sum=sum*i;
        i=i-1;
        i=digui(i);
    }
    else
    {
        return sum;
    }
}
int main()
{
    int a;
    long k=0;
    cin>>a;
    for(int i=1;i<=a;i++)
    {
        k=k+digui(i);
    }
    cout<<k;
}

by zhang_jun @ 2024-11-17 19:37:43

  1. 每次递归前需要将sum置为1
  2. 需要高精度

by ffgg57344 @ 2024-11-17 19:38:15

@zhang_jun 欧克谢谢


|