高精度计算1! 2! ....n! c
时间: 2023-12-02 12:03:04 浏览: 95
以下是Python实现高精度计算1! 2! ....n!的代码:
```python
def multiply(num1, num2):
"""
高精度乘法
"""
len1, len2 = len(num1), len(num2)
res = [0] * (len1 + len2)
for i in range(len1 - 1, -1, -1):
carry = 0
for j in range(len2 - 1, -1, -1):
tmp = int(num1[i]) * int(num2[j]) + carry + res[i + j + 1]
carry, res[i + j + 1] = divmod(tmp, 10)
res[i] += carry
while res[0] == 0 and len(res) > 1:
res.pop(0)
return ''.join(map(str, res))
def factorial(n):
"""
高精度阶乘
"""
res = '1'
for i in range(2, n + 1):
res = multiply(res, str(i))
return res
# 读取输入文件
with open('factor.in', 'r') as f:
n = int(f.readline().strip())
# 计算n的阶乘
res = factorial(n)
# 将结果写入输出文件
with open('factor.out', 'w') as f:
f.write(res)
```
阅读全文