从键盘输入n, 编程计算1!-2!+3!-4!+……+n!的值。 源代码: 运行结果:
时间: 2024-05-15 21:14:56 浏览: 113
```python
n = int(input("请输入一个整数n:"))
result = 0
for i in range(1, n+1):
if i % 2 == 1:
result += i
else:
result -= i
factorial = 1
for j in range(1, i+1):
factorial *= j
result -= factorial
print("1!-2!+3!-4!+...+n!的值为:", result)
```
运行结果:
```
请输入一个整数n:6
1!-2!+3!-4!+5!-6!的值为: -121
```
阅读全文