用Python实现求阶层(高精度)
代码如下
def level(n):
L=[]
L.append(1)
i=n
j=0
carry=0
while i>0:
j=0
k=len(L)
while k>0:
temp=L[j]*i+carry
if j+1>len(L)-1 and temp>=10:
L.append(0)
k=k+1
L[j]=temp%10
carry=int(temp/10)
j=j+1
k=k-1
i=i-1
L.reverse()
while L[0]==0:
L.pop(L[0])
for x in L:
print(x,end='')
def main():
n=int(input('请输入一个数:'))
level(n)
main()
测试截图
PS
- 由于求的数很大,一般的数据类型无法保存,这里用列表来保存
本文结束下面放一张好看的图片