Skip to content

Instantly share code, notes, and snippets.

@alldevic
Last active May 28, 2019 12:46
Show Gist options
  • Save alldevic/1a400a2ffd68558c4c4acde346d3bcfe to your computer and use it in GitHub Desktop.
Save alldevic/1a400a2ffd68558c4c4acde346d3bcfe to your computer and use it in GitHub Desktop.
using System;
namespace ConsoleApplication3
{
internal class Program
{
public static void Main(string[] args)
{
int n = 5, m = 3;
int[,] matrix = GenerateMatrix(n, m);
PrintMatrix(matrix);
Console.WriteLine("----- ----- -----");
SolveFirstTask(matrix);
Console.WriteLine("----- ----- -----");
SolveSecondTask(matrix);
Console.ReadKey();
}
public static int[,] GenerateMatrix(int n, int m)
{
int[,] matrix = new int[n, m];
Random rnd = new Random();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i, j] = rnd.Next(-10, 10);
}
}
return matrix;
}
public static void PrintMatrix(int[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + "\t");
}
Console.WriteLine();
}
}
public static void SolveFirstTask(int[,] matrix)
{
Console.WriteLine("Решение первой задачи начато");
int currNegativeCount = 0;
bool zeroExist = false;
bool rowExist = false;
for (int i = 0; i < matrix.GetLength(0); i++)
{
currNegativeCount = 0;
zeroExist = false;
for (int j = 0; j < matrix.GetLength(1); j++)
{
if (matrix[i, j] < 0)
{
currNegativeCount++;
}
if (matrix[i, j] == 0)
{
zeroExist = true;
rowExist = true;
}
}
if (zeroExist)
{
Console.WriteLine("Строка: " + (i + 1) + ", количество отрицательных элементов: " +
currNegativeCount);
}
}
if (!rowExist)
{
Console.WriteLine("Матрица не содержит необходимых строк");
}
Console.WriteLine("Решение первой задачи завершено");
}
public static void SolveSecondTask(int[,] matrix)
{
Console.WriteLine("Решение второй задачи начато");
int currMinRow = 0;
int currMaxColumn = 0;
bool pointExist = false;
for (int i = 0; i < matrix.GetLength(0); i++)
{
currMinRow = IndexMinInRow(matrix, i);
for (int j = 0; j < matrix.GetLength(1); j++)
{
currMaxColumn = IndexMaxInColumn(matrix, j);
if (matrix[i, currMinRow] == matrix[currMaxColumn, j])
{
pointExist = true;
Console.WriteLine("Найдена седловая точка: [" + i + ", " + j + "]");
break;
}
}
}
if (!pointExist)
{
Console.WriteLine("Седловых точек не найдено");
}
Console.WriteLine("Решение второй задачи заврешено");
}
public static int IndexMinInRow(int[,] matrix, int rowId)
{
int min = matrix[rowId, 0];
int minId = 0;
for (int i = 1; i < matrix.GetLength(1); i++)
{
if (matrix[rowId, i] < min)
{
min = matrix[rowId, i];
minId = i;
}
}
return minId;
}
public static int IndexMaxInColumn(int[,] matrix, int columnId)
{
int max = matrix[0, columnId];
int maxId = 0;
for (int i = 1; i < matrix.GetLength(0); i++)
{
if (matrix[i, columnId] > max)
{
max = matrix[i, columnId];
maxId = i;
}
}
return maxId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment