const n = 8, m = 8; var A: array[1..n,1..m] of integer; i, j, sum, product: integer; isnotnull: boolean; begin randomize; writeln('Случайная матрица:'); for i:=1 to n do begin for j:=1 to m do begin A[i,j] := random(51) - 25; write(A[i,j]:5); end; writeln; end; sum := 0; for i:=1 to n do if A[i,n-i+1] < 0 then sum := sum + A[i,n-i+1]; writeln('Сумма отрицательных элементов побочной диагонали = ', sum); product := 1; isnotnull := False; for i:=1 to n-1 do for j:=2 to n do if (j > i) and (A[i,j] <> 0) then begin isnotnull := True; product := product * A[i,j]; end; if isnotnull writeln('Произведение ненулевых элементов в области выше главной диагонали = ', product) else writeln('Ненулевых элементов в области выше главной диагонали нет.', product); readln; end.
Using System; using System.Linq; using System.Text;
namespace Test1 { class Program { static string file_name = "PrResh.txt"; static bool handfree = true; //определяет вручную ли вводятся данные или рандомом
static void Main() { Console.WriteLine("Введите размеры матрицы (строки x столбцы)"); int rows = Convert.ToInt32(Console.ReadLine()); int columns = Convert.ToInt32(Console.ReadLine());
var M = InitMatrix(rows, columns, handfree);
int Reply; do { Console.WriteLine(); Console.WriteLine("Выбирите метод решения:"); Console.WriteLine("1. Критерий Сэвиджа"); Console.WriteLine("2. Критерий Гермейера"); Console.WriteLine("3. Выход"); Reply = Convert.ToInt32(Console.ReadLine());
switch (Reply) { case 1: { SavageCriterion(M); break; } case 2: { var Q = new double[M.GetLength(1)]; if (handfree) { var r = new Random(DateTime.Now.Millisecond);
static double[,] InitMatrix(int rows, int columns, bool handfree = false) { var M = new double[rows, columns];
var sb = new StringBuilder(); sb.AppendLine("Матрица [" + rows + "x" + columns + "] :");
if (handfree) { var random = new Random(DateTime.Now.Millisecond); for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { M[i, j] = random.NextDouble() * random.Next(-50, 50); sb.Append(M[i, j] + " "); } sb.AppendLine(); } } else { for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { Console.Write("M[" + i + ", " + j + "] = "); M[i, j] = Convert.ToDouble(Console.Read()); sb.Append(M[i, j] + " "); } Console.WriteLine(); sb.AppendLine(); } }
OutputToConsoleAndLog(sb.ToString()); return M; }
static double SavageCriterion(double[,] M) { OutputToConsoleAndLog("Критерий Сэвиджа."); OutputToConsoleAndLog("Наибольшее значение каждого столбца."); var max = new double[M.GetLength(1)]; for (var j = 0; j < max.Length; j++) { max[j] = M[0, j]; for (var i = 1; i < M.GetLength(0); i++) { if (max[j] < M[i, j]) max[j] = M[i, j]; } OutputToConsoleAndLog("Max[" + j + " столбца] = " + max[j]);
}
OutputToConsoleAndLog("Вычтем из наибольшего значения столбца, каждое значение столбца."); OutputToConsoleAndLog("Сформируем новую матрицу из полученных значений."); var sb = new StringBuilder();
for (var i = 0; i < M.GetLength(0); i++) { for (var j = 0; j < M.GetLength(1); j++) { M[i, j] = max[j] - M[i, j]; sb.Append(M[i, j] + " "); } sb.AppendLine(); } OutputToConsoleAndLog(sb.ToString());
OutputToConsoleAndLog("Наибольшее значение каждой строки."); max = new double[M.GetLength(0)];
for (var i = 0; i < M.GetLength(0); i++) { max[i] = M[i, 0]; for (var j = 0; j < M.GetLength(1); j++) { if (max[i] < M[i, j]) max[i] = M[i, j]; } OutputToConsoleAndLog("Max[" + i + " строки] = " + max[i]); }
static double GermeierCriterion(double[,] M, double[] Q) { OutputToConsoleAndLog("Критерий Гермейера."); OutputToConsoleAndLog("Для решения необходимы сведенья о вероятности принятия каждого решения."); OutputToConsoleAndLog("Вероятности принятия решения:"); for (var j = 0; j < Q.Length; j++) { OutputToConsoleAndLog("Q[" + j + "] = " + Q[j]); }
var max = M[0, 0]; for (var i = 0; i < M.GetLength(0); i++) { for (var j = 0; j < M.GetLength(1); j++) { if (max < M[i, j]) max = M[i, j]; } }
OutputToConsoleAndLog("Наибольшее значение в матрице = " + max); max += 1; OutputToConsoleAndLog("Необходимо из каждого элемента матрицы вычесть " + max); OutputToConsoleAndLog("Полученная матрица:"); var sb = new StringBuilder();
for (var i = 0; i < M.GetLength(0); i++) { for (var j = 0; j < M.GetLength(1); j++) { M[i, j] -= max; sb.Append(M[i, j] + " "); M[i, j] *= Q[j]; } sb.AppendLine(); } OutputToConsoleAndLog(sb.ToString());
OutputToConsoleAndLog("Умножаем каждый элемент матрицы на соответствующую вероятность."); OutputToConsoleAndLog("Выбираем наименьший результат каждой строки."); var min = new double[M.GetLength(0)];
for (var i = 0; i < M.GetLength(0); i++) { M[i, 0] *= Q[0]; min[i] = M[i, 0]; for (var j = 1; j < M.GetLength(1); j++) { M[i, j] *= Q[j]; if (min[i] > M[i, j]) min[i] = M[i, j]; } OutputToConsoleAndLog("Min[" + i + " строки] = " + min[i]); }
что-то типа этого:
const n = 8, m = 8;
var
A: array[1..n,1..m] of integer;
i, j, sum, product: integer;
isnotnull: boolean;
begin
randomize;
writeln('Случайная матрица:'); for i:=1 to n do begin
for j:=1 to m do begin
A[i,j] := random(51) - 25;
write(A[i,j]:5);
end;
writeln;
end; sum := 0;
for i:=1 to n do
if A[i,n-i+1] < 0 then
sum := sum + A[i,n-i+1];
writeln('Сумма отрицательных элементов побочной диагонали = ', sum); product := 1;
isnotnull := False;
for i:=1 to n-1 do
for j:=2 to n do
if (j > i) and (A[i,j] <> 0) then begin
isnotnull := True;
product := product * A[i,j];
end;
if isnotnull
writeln('Произведение ненулевых элементов в области выше главной диагонали = ', product)
else
writeln('Ненулевых элементов в области выше главной диагонали нет.', product); readln;
end.