
TQC+ 程式語言Python 606 二維串列行列數
題目說明:
請撰寫一程式,讓使用者輸入兩個正整數rows、cols,分別表示二維串列lst 的「第一個維度大小」與「第二個維度大小」。
串列元素[row][col]所儲存的數字,其規則為:row、col 的交點值 = 第二個維度的索引col – 第一個維度的索引row。
接著以該串列作為參數呼叫函式compute()輸出串列。
串列元素[row][col]所儲存的數字,其規則為:row、col 的交點值 = 第二個維度的索引col – 第一個維度的索引row。
接著以該串列作為參數呼叫函式compute()輸出串列。
提示:欄寬為4。
範例輸入
5
10
範例輸出
0 1 2 3 4 5 6 7 8 9
-1 0 1 2 3 4 5 6 7 8
-2 -1 0 1 2 3 4 5 6 7
-3 -2 -1 0 1 2 3 4 5 6
-4 -3 -2 -1 0 1 2 3 4 5
程式碼:
def compute(lst):
for row in range(rows):
for col in range(cols):
print('%4d' %lst[row][col], end='')
print('')
rows = eval(input())
cols = eval(input())
lst = []
for row in range(rows):
lst.append([])
for col in range(cols):
lst[row].append(col - row)
compute(lst)
TQC+ 程式語言Python 第6類:串列(List)的運作(一維、二維以及多維)
如果覺得文章內容還不錯的話,麻煩請幫我點個讚!感謝
可以多點幾次喔~~
第一次點讚需使用 Google 或 Facebook 帳號註冊

