n = int(input()) matrix = [] for i in range(n): row = list(map(float, input().split())) matrix.append(row) sum1 = sum(matrix[i][i] for i in range(n)) # 主对角线上的元素之和 sum2 = sum(matrix[i][n-i-1] for i in range(n)) # 副对角线上的元素之和 print("sum1={:.2f}, sum2={:.2f}".format(sum1, sum2))将这串代码改为求sum1sum2d 的和
时间: 2024-01-24 14:17:57 浏览: 34
n = int(input())
matrix = []
for i in range(n):
row = list(map(float, input().split()))
matrix.append(row)
sum1 = sum(matrix[i][i] for i in range(n)) # 主对角线上的元素之和
sum2 = sum(matrix[i][n-i-1] for i in range(n)) # 副对角线上的元素之和
d = sum(matrix[i][j] for i in range(n) for j in range(n) if i != j and i+j != n-1) # 其它元素之和
print("sum1={:.2f}, sum2={:.2f}, d={:.2f}".format(sum1, sum2, d))
# 注意:这里直接将求和的表达式放入格式化字符串中,不需要另外定义变量
相关问题
编写函数,分别实现如下功能: 一。从键盘输入一个int类型的n阶方阵(n<10)存入二维数组中。 二。求转置矩阵并输出。 三。判断矩阵是否是对称矩阵。 四。找出矩阵中的最大值和最小值,并输出到屏幕。 五。对矩阵每一行进行升序排序后存入另个二维数组中,然后输出到屏幕。 六。求矩阵的行列式 (采用递归或非递归方式)。
下面是实现上述功能的代码:
```python
# 从键盘输入一个int类型的n阶方阵(n<10)存入二维数组中
def input_matrix(n):
matrix = []
for i in range(n):
row = list(map(int, input().split()))
matrix.append(row)
return matrix
# 求转置矩阵并输出
def transpose_matrix(matrix):
rows = len(matrix)
cols = len(matrix[0])
transpose = [[matrix[j][i] for j in range(rows)] for i in range(cols)]
return transpose
# 判断矩阵是否是对称矩阵
def is_symmetric_matrix(matrix):
rows = len(matrix)
cols = len(matrix[0])
if rows != cols:
return False
for i in range(rows):
for j in range(i+1, cols):
if matrix[i][j] != matrix[j][i]:
return False
return True
# 找出矩阵中的最大值和最小值,并输出到屏幕
def find_max_min(matrix):
max_val = float('-inf')
min_val = float('inf')
for row in matrix:
for val in row:
if val > max_val:
max_val = val
if val < min_val:
min_val = val
print(f"Max value: {max_val}")
print(f"Min value: {min_val}")
# 对矩阵每一行进行升序排序后存入另个二维数组中,然后输出到屏幕
def sort_rows(matrix):
sorted_matrix = []
for row in matrix:
sorted_row = sorted(row)
sorted_matrix.append(sorted_row)
return sorted_matrix
# 求矩阵的行列式 (采用递归方式)
def determinant(matrix):
size = len(matrix)
if size == 1:
return matrix[0][0]
elif size == 2:
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]
else:
det = 0
for i in range(size):
minor = []
for j in range(1, size):
row = []
for k in range(size):
if k != i:
row.append(matrix[j][k])
minor.append(row)
sign = (-1)**i
det += sign*matrix[0][i]*determinant(minor)
return det
# 从键盘输入一个int类型的n阶方阵(n<10)存入二维数组中
n = int(input("Enter n: "))
print("Enter the matrix:")
matrix = input_matrix(n)
# 求转置矩阵并输出
print("Transpose matrix:")
transpose = transpose_matrix(matrix)
for row in transpose:
print(row)
# 判断矩阵是否是对称矩阵
if is_symmetric_matrix(matrix):
print("Matrix is symmetric")
else:
print("Matrix is not symmetric")
# 找出矩阵中的最大值和最小值,并输出到屏幕
find_max_min(matrix)
# 对矩阵每一行进行升序排序后存入另个二维数组中,然后输出到屏幕
print("Sorted matrix:")
sorted_matrix = sort_rows(matrix)
for row in sorted_matrix:
print(row)
# 求矩阵的行列式 (采用递归方式)
det = determinant(matrix)
print(f"Determinant: {det}")
```
注意:如果要求逆矩阵,可以使用 `numpy` 库中的 `linalg.inv()` 函数。
Solve the problem with code:Ack Country has discovered large quantities of crude oil under its territory. The king decided that all n industrial facilities in the country should be connected with crude oil to keep production working. There are two ways to supply crude oil to industrial facilities, either by laying pipelines to other industrial facilities that are already supplied with crude oil, or by exploring for oil at the plant site.The cost of crude oil exploration is w, and the cost of connecting two industrial facilities is c, given in matrix form, and the main diagonal of matrix is 0. Calculate the minimum cost of supplying crude oil to all industrial facilities.
Sure, here is a Python code that solves the problem using the Floyd-Warshall algorithm:
```python
INF = float('inf')
def find_min_cost(n, c, w):
for k in range(n):
for i in range(n):
for j in range(n):
c[i][j] = min(c[i][j], c[i][k] + c[k][j])
w[i][j] = min(w[i][j], c[i][j] + min(w[i][k], w[k][j]))
return sum(sum(row) for row in w)
n = int(input())
c = []
w = []
for i in range(n):
c_row = list(map(int, input().split()))
c.append(c_row)
w_row = [INF if x == 0 else x for x in c_row]
w.append(w_row)
print(find_min_cost(n, c, w))
```
This code takes the input of `n`, the size of the matrix, `c`, the cost of connecting two industrial facilities, and `w`, the cost of crude oil exploration. It then initializes the `INF` constant to represent infinity and sets the diagonal of `w` to infinity since exploration is not required for facilities that already have oil.
The algorithm then uses three nested loops to calculate the minimum cost of connecting all the facilities. It first calculates the shortest path between two facilities using the `c` matrix with the Floyd-Warshall algorithm. It then calculates the cost of supplying oil to each facility by adding the cost of connecting the facility to the nearest facility with oil and the cost of exploration if necessary.
Finally, the algorithm returns the total minimum cost of supplying oil to all facilities by summing up the values in the `w` matrix.
Please note that this code assumes that the input is valid and follows the problem statement. It also assumes that the input is formatted correctly.