Упрощаем, пишем функцию для поиска произведения членов списка, конкретно для данной задачи для одного столбца
import math
def column_mul(matrix, number):
"""Возвращает произведение членов столбца с номером number"""
column = [row[number] for row in matrix] # столбец с номером number
return math.prod(column)
Далее используем данную функцию для количества столбцов и храним результат в переменной mul_all_columns
, а затем используем метод min
mul_all_columns = []
for i in range(len(matrix)):
mul_all_columns.append(column_mul(matrix, i))
minimum = min(mul_all_columns)
либо можно сократить до одной строки
minimum = min(column_mul(matrix, i) for i in range(len(matrix)))
также можно и максимум найти
maximum = max(column_mul(matrix, i) for i in range(len(matrix)))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
uses crt; const nmax=20; type matr=array [1..nmax,1..nmax] of integer; mas=array [1..nmax] of integer; var a: matr; a1,a2,a3,a4: mas; n,m: byte; procedure Vvod (var z: matr; var ri,rj: byte); var i,j: byte; begin repeat write('Kol-vo strok, ne menee 4, ne bolee ',nmax,', n='); readln(ri); until ri in [4..nmax]; repeat write('Kol-vo stolbzov, ne bolee ',nmax,', n='); readln(rj); until rj in [1..nmax]; writeln('Vvedite elementy matrizy:'); for i:=1 to ri do for j:=1 to rj do begin write('a[',i,',',j,']='); readln(z[i,j]); end; end; procedure Vivod (z: matr; ri,rj: byte); var i,j: byte; begin writeln('Ischodnaja matriza'); for i:=1 to ri do begin for j:=1 to rj do write(z[i,j]:4); writeln; end; end; procedure Vivod1M (c: char; z: mas; rj: byte); var j: byte; begin writeln('Massiv ',c); for j:=1 to rj do write(z[j]:4); writeln; end; procedure MakeArray (ns: byte; x: matr; var y: mas; rj: byte); var j: byte; begin for j:=1 to rj do y[j]:=x[ns,j]; end; function Jmin (z: mas; rj: byte): byte; var j,jm: byte; begin jm:=1; for j:=1 to rj do if z[j]<z[jm] then jm:=j; Jmin:=jm; end; procedure Zamena (var z: mas; rj: byte); var j: byte; g: integer; begin g:=z[Jmin(z,rj)]; if Jmin(z,rj)<rj then for j:=Jmin(z,rj)+1 to rj do z[j]:=g; end; begin Vvod (a,n,m); clrscr; Vivod (a,n,m); MakeArray (1,a,a1,m); MakeArray (2,a,a2,m); MakeArray (3,a,a3,m); MakeArray (4,a,a4,m); writeln('Massivy do preobrazovania'); Vivod1M ('1',a1,m); Vivod1M ('2',a2,m); Vivod1M ('3',a3,m); Vivod1M ('4',a4,m); Zamena (a1,m); Zamena (a2,m); Zamena (a3,m); Zamena (a4,m); writeln('Massivy posle zameny'); Vivod1M ('1',a1,m); Vivod1M ('2',a2,m); Vivod1M ('3',a3,m); Vivod1M ('4',a4,m); readln; end. |
uses crt; const nmax=20; type matr=array [1..nmax,1..nmax] of integer; mas=array [1..nmax] of integer; var a: matr; a1,a2,a3,a4: mas; n,m: byte; procedure Vvod (var z: matr; var ri,rj: byte); var i,j: byte; begin repeat write('Kol-vo strok, ne menee 4, ne bolee ',nmax,', n='); readln(ri); until ri in [4..nmax]; repeat write('Kol-vo stolbzov, ne bolee ',nmax,', n='); readln(rj); until rj in [1..nmax]; writeln('Vvedite elementy matrizy:'); for i:=1 to ri do for j:=1 to rj do begin write('a[',i,',',j,']='); readln(z[i,j]); end; end; procedure Vivod (z: matr; ri,rj: byte); var i,j: byte; begin writeln('Ischodnaja matriza'); for i:=1 to ri do begin for j:=1 to rj do write(z[i,j]:4); writeln; end; end; procedure Vivod1M (c: char; z: mas; rj: byte); var j: byte; begin writeln('Massiv ',c); for j:=1 to rj do write(z[j]:4); writeln; end; procedure MakeArray (ns: byte; x: matr; var y: mas; rj: byte); var j: byte; begin for j:=1 to rj do y[j]:=x[ns,j]; end; function Jmin (z: mas; rj: byte): byte; var j,jm: byte; begin jm:=1; for j:=1 to rj do if z[j]<z[jm] then jm:=j; Jmin:=jm; end; procedure Zamena (var z: mas; rj: byte); var j: byte; g: integer; begin g:=z[Jmin(z,rj)]; if Jmin(z,rj)<rj then for j:=Jmin(z,rj)+1 to rj do z[j]:=g; end; begin Vvod (a,n,m); clrscr; Vivod (a,n,m); MakeArray (1,a,a1,m); MakeArray (2,a,a2,m); MakeArray (3,a,a3,m); MakeArray (4,a,a4,m); writeln('Massivy do preobrazovania'); Vivod1M ('1',a1,m); Vivod1M ('2',a2,m); Vivod1M ('3',a3,m); Vivod1M ('4',a4,m); Zamena (a1,m); Zamena (a2,m); Zamena (a3,m); Zamena (a4,m); writeln('Massivy posle zameny'); Vivod1M ('1',a1,m); Vivod1M ('2',a2,m); Vivod1M ('3',a3,m); Vivod1M ('4',a4,m); readln; end.
Ученик
(215),
закрыт
4 года назад
Кириллов Сергей
Мудрец
(15232)
4 года назад
var i, j, k, m, n, pro: integer;
a: array [1..100,1..100] of byte;
begin
write (‘n, m = ‘);
readln (n, m);
k:=1+random (m);
writeln (‘k = ‘,k);
pro:=1;
for i:=1 to n do
begin
for j:=1 to m do
begin
a[i,j]:=1+random(9);
write (a[i, j]:2);
if j = k then pro:=pro*a[i,j];
end;
writeln;
end;
writeln (‘Произведение столбца (‘,k,’) = ‘, pro);
end.
Start with the declaration: make sure that your program works with m×n matrix, not simply a 3×3 matrix. Since m and n have limits of 10 and 20, and because you must add an extra row and a column to the result, the declaration should be
int a[11][21];
You also need to declare m
and n
, have end-user enter them, and validate them to be within their acceptable ranges:
int m, n;
cin >> m >> n;
... // Do the validation
Now you can rewrite your loops in terms of m
and n
, rather than using 3
throughout your code.
With these declarations in place, you can total the numbers in place, i.e. for each row r
you would write
for (int i = 0 ; i != n ; i++) {
a[r][n+1] += a[r][i];
}
Similarly, you would compute the product (don’t forget to start it with the initial value of 1
, not 0
).
At the end you would print an (m+1)×(n+1) matrix to complete the task.