Everyday life is different,even with your state and mind!So if i have some new ways or logic to make a good Algorithms,I gonna post it and share with U guys! If there is anything error aboubt what I demonstrated,pls speak out on the comment,Thanks!
一.最初的方法
不知道你是否还记得,当你第一次学习如何使用编程语言写出阶乘的时候,你需要使用两层循环,而你算出一个的时候,都会重复计算子问题,这就导致了重复计算,所需时间会随着计算规模增大而增大。
from time import time
num = int(input())
dp1 = [0] * (num+1)
start = time()
for i in range(num+1):
fac = 1
for j in range(i,1,-1):
fac*=j
dp1[i]=fac
end = time()
waste = end - start
print(dp1)
print(f"两层For循环所消耗的时间:{waste}")
二.新的出路