算法(ALGORITHMS)---- 关于阶乘

 

        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}")

 

二.新的出路 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值