题目要求-|||-对于给定的正整数N,需要你计算+S=1!+2!+3!+···+N1/-|||-输入恪式:-|||-输入在一行中给出个不超过20的正整数N。-|输出格式:在一行中输出S++java
时间: 2024-01-05 22:20:15 浏览: 64
根据提供的引用内容,这个题目要求计算阶乘的和。下面是两种不同语言的解法:
1. Python解法:
```python
def factorial(n):
ans = 1
for i in range(1, n+1):
ans *= i
return ans
N = int(input())
S = 0
for i in range(1, N+1):
S += factorial(i)
print(S)
```
2. Java解法:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
long S = 0;
for (int i = 1; i <= N; i++) {
S += factorial(i);
}
System.out.println(S);
}
public static long factorial(int n) {
long ans = 1;
for (int i = 1; i <= n; i++) {
ans *= i;
}
return ans;
}
}
```
阅读全文