請問這三個python程式碼怎麼寫
- 2022-07-24
金字塔演算法:
CONST_ROW = 8
CONST_TAB = “\t”
lstRow = []
# /* 遍歷列印的行數。 */
for intRow in range(1, CONST_ROW + 1):
# /* 遍歷列印的列數 (折半+1)。 */
for intCol in range(1, intRow + 1):
# /* 每次僅計算最後一個數並追加到列表末尾。
# 避免對中部之前的每一行每一列的重複數值計算,
# 一定程度上能提高執行效率。 */
if intCol == intRow:
lstRow。append(2 ** (intCol - 1))
strOut = CONST_TAB * (CONST_ROW - intRow) + \
CONST_TAB。join(map(str, lstRow))
# /* 從第二行開始需要執行列表翻轉 (補齊折半-1)。 */
if intRow > 1:
strOut = “%s%s%s” % (strOut,
CONST_TAB,
CONST_TAB。join(map(str, reversed(lstRow[:-1]))))
print(strOut)輸出:
其他兩題建議自己思考,自己練習,這樣才能提高!