对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!。pta
时间: 2023-12-08 17:38:59 浏览: 125
以下是C语言的代码实现:
```c
#include <stdio.h>
int factorial(int n);
int main(void) {
int n, sum, i;
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; i++) {
sum += factorial(i);
}
printf("%d", sum);
return 0;
}
int factorial(int n) {
int i, m;
m = 1;
for (i = 1; i <= n; i++) {
m *= i;
}
return m;
}
```
以下是Python的代码实现:
```python
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
n = int(input())
sum = 0
for i in range(1, n+1):
sum += factorial(i)
print(sum)
```
阅读全文