Find the sum of the factorial, pay attention to the type of sum, double type will have floating point difference


#include<iostream>
#include<cmath>
using namespace std;
int js(int n);
int main()
{
    
    
int n;
long long sum=0;
cin >> n;
for(int i=1;i<=n;i++)
{
    
    sum+=js(i);}
cout <<sum<< endl;
return 0;
}

int js(int n)//定义函数体
{
    
    
int s=1;
for(int i=1;i<=n;i++)
s*=i;
return s;//函数的返回值
}

008: Find the
sum of factorials Total time limit: 1000ms Memory limit: 65536kB
Description: Given a positive integer n, find the sum of factorials of positive integers not greater than n (that is, find 1!+2!+3!+…+n! ) Input There is one line of input, containing a positive integer n (1 <n <12). The output output has one line: the sum of factorials.
Sample input
5
Sample output
153
Introduction to source calculation 05

Guess you like

Origin blog.csdn.net/qq_51082388/article/details/113066629