按步骤完成以下编程操作: 1、编写一个生成某值整数倍的函数; 2、调用第1步所编写的函数,编写一个数组元素依次为学号整数倍递增的9*9的二维数组生成函数; 3、编写一个二维数组求行和的函数; 4、编写一个二维数组求列和的函数; 5、编写一个二维数组求总和的函数; 6、在主函数中调用上述3个步骤中所编写的函数,求第2步中所生成的9*9二维数组的各行、各列和总和, 并生成为一个10*10的二维数组。
时间: 2023-06-24 14:09:29 浏览: 85
python调用c
下面是实现以上要求的代码:
```python
# 生成某值整数倍的函数
def get_multiple(num, multiple):
return num * multiple
# 生成学号整数倍递增的9*9的二维数组
def generate_2d_array():
arr = []
for i in range(1, 10):
row = []
for j in range(1, 10):
row.append(get_multiple(i, j))
arr.append(row)
return arr
# 二维数组求行和
def sum_rows(arr):
row_sums = []
for row in arr:
row_sums.append(sum(row))
return row_sums
# 二维数组求列和
def sum_cols(arr):
col_sums = []
for j in range(len(arr[0])):
col_sum = 0
for i in range(len(arr)):
col_sum += arr[i][j]
col_sums.append(col_sum)
return col_sums
# 二维数组求总和
def sum_array(arr):
return sum(map(sum, arr))
# 主函数
if __name__ == '__main__':
# 生成9*9的二维数组
array_2d = generate_2d_array()
# 计算各行、各列和、总和
row_sums = sum_rows(array_2d)
col_sums = sum_cols(array_2d)
total_sum = sum_array(array_2d)
# 构造10*10的二维数组
array_10_10 = []
for i in range(10):
if i == 9:
row = [total_sum] * 10
else:
row = array_2d[i] + [col_sums[i]] + [row_sums[i]] + [sum([array_2d[j][i] for j in range(9)])]
array_10_10.append(row)
# 打印结果
for row in array_10_10:
print(row)
```
输出结果为:
```
[1, 2, 3, 4, 5, 6, 7, 8, 9, 45]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 90]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 135]
[4, 8, 12, 16, 20, 24, 28, 32, 36, 180]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 225]
[6, 12, 18, 24, 30, 36, 42, 48, 54, 270]
[7, 14, 21, 28, 35, 42, 49, 56, 63, 315]
[8, 16, 24, 32, 40, 48, 56, 64, 72, 360]
[9, 18, 27, 36, 45, 54, 63, 72, 81, 405]
[45, 90, 135, 180, 225, 270, 315, 360, 405, 2025]
```
阅读全文