构造一个3行3列的矩阵,从键盘随机输入每个元素的值,分别求:所有元素的和、上三角的和、下三角的和、对角线的和、最小值及最小值对应的行列下标。
时间: 2023-04-24 09:06:47 浏览: 173
矩阵为:
a11 a12 a13
a21 a22 a23
a31 a32 a33
求解:
1. 所有元素的和:sum = a11 + a12 + a13 + a21 + a22 + a23 + a31 + a32 + a33
2. 上三角的和:sum_up = a12 + a13 + a23
3. 下三角的和:sum_down = a21 + a31 + a32
4. 对角线的和:sum_diag = a11 + a22 + a33
5. 最小值及最小值对应的行列下标:
min_val = a11
min_row = 1
min_col = 1
if a12 < min_val:
min_val = a12
min_row = 1
min_col = 2
if a13 < min_val:
min_val = a13
min_row = 1
min_col = 3
if a21 < min_val:
min_val = a21
min_row = 2
min_col = 1
if a22 < min_val:
min_val = a22
min_row = 2
min_col = 2
if a23 < min_val:
min_val = a23
min_row = 2
min_col = 3
if a31 < min_val:
min_val = a31
min_row = 3
min_col = 1
if a32 < min_val:
min_val = a32
min_row = 3
min_col = 2
if a33 < min_val:
min_val = a33
min_row = 3
min_col = 3
阅读全文