矩阵主对角线下方的元素总和 python
Sum of elements below the main diagonal of a matrix python
基本上我必须编写一个程序来创建一个 N × N 的矩阵并用随机数填充它并计算矩阵主对角线下方所有元素的总和。除了元素的总和,我已经做了所有的事情,这就是我所拥有的
import random
def llenar_matriz(n):
# Fills the matrix with random numbers between 1 and 99
for r in range(n):
fila = []
for c in range(n):
fila.append(random.randint(1,99))
matriz.append(fila)
return matriz
def imprimir_matriz(matriz):
# Prints the matrix properly (makes every list go below the previous one)
filas = len(matriz)
columnas = len(matriz[0])
for f in range(filas):
for c in range(columnas):
print ("%3d" %matriz[f][c], end="")
print()
def suma_matriz(matriz):
#This should make the sum of all the values below the main diagonal of the matrix
# Programa principal
lado = int(input("Ingrese el ancho de la matriz: ")) #input value that makes the matrix N by N
matriz = []
llenar_matriz(lado)
imprimir_matriz(matriz)
total = suma_matriz()
print("La suma de los numeros debajo de la diagonal principal de la matriz es: ",total)
我需要帮助的是接收矩阵并对主对角线下方的所有值求和的函数
试试这个
import random
def llenar_matriz(n):
# Fills the matrix with random numbers between 1 and 99
matriz = []
for r in range(n):
fila = []
for c in range(n):
fila.append(random.randint(1,99))
matriz.append(fila)
return matriz
def imprimir_matriz(matriz):
filas = len(matriz)
columnas = len(matriz[0])
for f in range(filas):
for c in range(columnas):
print ("%3d" % matriz[f][c], end="")
print()
def suma_matriz(matriz):
#This should make the sum of all the values below the main diagonal of the matrix
sum = 0
for i in range(len(matriz)):
for j in range(len(matriz)):
if i != j and i > j:
sum += matriz[i][j]
return sum
# Programa principal
lado = int(input("Ingrese el ancho de la matriz: ")) #input value that makes the matrix N by N
matriz = llenar_matriz(lado)
imprimir_matriz(matriz)
total = suma_matriz(matriz)
print("La suma de los numeros debajo de la diagonal principal de la matriz es: ",total)
给出输出
Ingrese el ancho de la matriz: 5
77 22 19 46 77
32 14 39 2 61
92 26 54 56 92
24 73 37 40 16
27 34 51 35 39
La suma de los numeros debajo de la diagonal principal de la matriz es: 431
基本上我必须编写一个程序来创建一个 N × N 的矩阵并用随机数填充它并计算矩阵主对角线下方所有元素的总和。除了元素的总和,我已经做了所有的事情,这就是我所拥有的
import random
def llenar_matriz(n):
# Fills the matrix with random numbers between 1 and 99
for r in range(n):
fila = []
for c in range(n):
fila.append(random.randint(1,99))
matriz.append(fila)
return matriz
def imprimir_matriz(matriz):
# Prints the matrix properly (makes every list go below the previous one)
filas = len(matriz)
columnas = len(matriz[0])
for f in range(filas):
for c in range(columnas):
print ("%3d" %matriz[f][c], end="")
print()
def suma_matriz(matriz):
#This should make the sum of all the values below the main diagonal of the matrix
# Programa principal
lado = int(input("Ingrese el ancho de la matriz: ")) #input value that makes the matrix N by N
matriz = []
llenar_matriz(lado)
imprimir_matriz(matriz)
total = suma_matriz()
print("La suma de los numeros debajo de la diagonal principal de la matriz es: ",total)
我需要帮助的是接收矩阵并对主对角线下方的所有值求和的函数
试试这个
import random
def llenar_matriz(n):
# Fills the matrix with random numbers between 1 and 99
matriz = []
for r in range(n):
fila = []
for c in range(n):
fila.append(random.randint(1,99))
matriz.append(fila)
return matriz
def imprimir_matriz(matriz):
filas = len(matriz)
columnas = len(matriz[0])
for f in range(filas):
for c in range(columnas):
print ("%3d" % matriz[f][c], end="")
print()
def suma_matriz(matriz):
#This should make the sum of all the values below the main diagonal of the matrix
sum = 0
for i in range(len(matriz)):
for j in range(len(matriz)):
if i != j and i > j:
sum += matriz[i][j]
return sum
# Programa principal
lado = int(input("Ingrese el ancho de la matriz: ")) #input value that makes the matrix N by N
matriz = llenar_matriz(lado)
imprimir_matriz(matriz)
total = suma_matriz(matriz)
print("La suma de los numeros debajo de la diagonal principal de la matriz es: ",total)
给出输出
Ingrese el ancho de la matriz: 5
77 22 19 46 77
32 14 39 2 61
92 26 54 56 92
24 73 37 40 16
27 34 51 35 39
La suma de los numeros debajo de la diagonal principal de la matriz es: 431