Python 计算S=1!-2!+3!-4!+5!-……n!
时间: 2024-01-02 19:22:43 浏览: 96
k-menas++_pythonk-means++_yolov4_源码
5星 · 资源好评率100%
以下是Python计算S=1!-2!+3!-4!+5!-……n!的例子:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def calculate_S(n):
S = 0
for i in range(1, n+1):
if i % 2 == 0:
S -= factorial(i)
else:
S += factorial(i)
return S
n = int(input("请输入n的值:"))
result = calculate_S(n)
print("计算结果为:", result)
```
阅读全文