Max continues matrix
def matr_sum(mat:list):
s = 0
for row in mat:
for el in row:
s += el
return s
def sub_size(length:int):
for start in range(length):
for end in range(start+1, length+1):
yield (start, end)
def max_matr_sum(matr:list, m:int, n:int):
max_s = 0
max_m = matr
for start_x, end_x in sub_size(m):
for start_y, end_y in sub_size(n):
tmp_matr = [i[start_y:end_y] for i in matr[start_x:end_x]]
if matr_sum(tmp_matr) > max_s:
max_s = matr_sum(tmp_matr)
max_m = tmp_matr
print(max_s)
for el in max_m:
print(*el)
while True:
m, n = (map(int, input('m n ').split()))
matrix = list([] for i in range(n))
i = 0
while i < m:
try:
tmp = list(map(int, input().split()))
if len(tmp) != n:
raise ValueError
matrix[i] = tmp
i += 1
except ValueError:
print('incorrecr input')
continue
max_matr_sum(matrix, m, n)
Comments
Leave a comment