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); for (var j = 0; j < Q.Length; j++) { Q[j] = r.NextDouble(); } } else { Console.WriteLine("Введите вероятности"); for (var j = 0; j < Q.Length; j++) { Q[j] = Convert.ToDouble(Console.Read()); } } GermeierCriterion(M, Q); break; } default: { Reply = -1; break; } } } while (Reply > 0); Console.ReadKey(); } 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]); } OutputToConsoleAndLog("Ответ: " + max.Last()); return max.Last(); } 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]); } OutputToConsoleAndLog("Ответ: " + min.Max()); return min.Max(); } static void OutputToConsoleAndLog(string text) { Console.WriteLine(text); using (var file = new System.IO.StreamWriter(file_name, true)) { file.WriteLine(text); } } }}