日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

帕斯卡三角形html,Python实现的帕斯卡三角形

發布時間:2025/3/19 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 帕斯卡三角形html,Python实现的帕斯卡三角形 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

帶有緩存的迭代器

# 帕斯卡三角形

mCache = {}

def pascalWithDict(n,k):

if n==k or k==0 or n==1:

return 1

if k==1:

return n

if mCache.has_key((n,k)):

return mCache[(n,k)]

else:

mCache[n,k] = pascalWithDict(n-1,k-1)+pascalWithDict(n-1,k)

return mCache[n,k]

## 獲得每行pascal列表

def generatePascal(depth):

lines = []

for row in range(depth):

line = []

for col in range(row+1):

line.append(pascal(row, col))

lines.append(line)

return lines

if __name__ == "__main__":

high = int(raw_input("pls enter the height of pascal:"))

lines = generatePascal(high)

for i in range(high):

print lines[i]

結果如下:

pls enter the height of pascal:6

[1]

[1, 1]

[1, 2, 1]

[1, 3, 3, 1]

[1, 4, 6, 4, 1]

[1, 5, 10, 10, 5, 1]

使用裝飾器的迭代器

from functools import wraps

def memo(func):

cache={}

@wraps(func)

def wrap(*args):

if args not in cache:

cache[args]=func(*args)

return cache[args]

return wrap

@memo

def pascal(n,k):

if n==k or k==0 or n==1:

return 1

if k==1:

return n

return pascal(n-1,k-1)+pascal(n-1,k)

跳舞的數字[Just for fun]

if __name__ == "__main__":

#depth = int(raw_input("pls enter the depth of pascal:"))

for depth in range(100):

lines = generatePascal(depth)

for i in range(depth):

print lines[i]

總結

以上是生活随笔為你收集整理的帕斯卡三角形html,Python实现的帕斯卡三角形的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。