Найти отрицательные элементы в строках, содержащих нулевой элемент и седловые точки матрицы - C#

Узнай цену своей работы

Формулировка задачи:

Дана целочисленная прямоугольная матрица. Определить: 1) количество отрицательных элементов в тех строках, которые содержат хотя бы один нулевой элемент. 2) номера строк и столбцов всех седловых точек матрицы Я написал формулу но не могу найти ошибку, "номера строк и столбцов всех седловых точек матрицы" не показывает, помогите пожалуйста, заранее спасибо
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Labka_6
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int[,] mas = new int[5, 5];
  13. Random rnd = new Random();
  14. for (int i = 0; i < 5; i++)
  15. {
  16. for (int j = 0; j < 5; j++)
  17. {
  18. mas[i, j] = -5 + rnd.Next(11);
  19. Console.Write(mas[i, j] + " ");
  20. }
  21. Console.WriteLine();
  22. }
  23. int count;//используется для подсчета количества отрицательных элементов
  24. for (int i = 0; i < 5; i++)
  25. {
  26. count = 0;
  27. for (int j = 0; j < 5; j++)
  28. {
  29. if (mas[i, j] == 0)
  30. {
  31. for (int k = 0; k < 5; k++)
  32. {
  33. if (mas[i, k] < 0)
  34. count++;
  35. }
  36. Console.WriteLine("В {0} строке количество отрицательных элементов = {1}", (i + 1), count);
  37. break;
  38. }
  39. }
  40. }
  41. for (int i = 0; i < 5; i++)
  42. {
  43. for (int j = 0; j < 5; j++)
  44. Console.Write("");
  45. Console.WriteLine();
  46. }
  47. //2. Номера строк и столбцов, всех седловых точек:
  48. Console.WriteLine("Номера строк и столбцов всех седловых точек:");
  49. for (int i = 0; i < 5; i++)
  50. {
  51. for (int j = 0; j < 5; j++)
  52. {
  53. bool flag = true; //идти далее
  54. //минимальный ли в своей строке
  55. for (int jj = 0; jj < 5; jj++)
  56. if (mas[i, j] > mas[i, jj])
  57. {
  58. flag = false;
  59. break;
  60. }
  61. if (flag)
  62. {
  63. //максимальный ли в своем столбце
  64. for (int ii = 0; ii < 5; ii++)
  65. if (mas[i,j] < mas[ii, j])
  66. {
  67. flag = false;
  68. break;
  69. }
  70. if (flag)
  71. Console.WriteLine(mas[i, j] + "[" + i + ";" + j + "]");
  72. }
  73. }
  74. }
  75. Console.ReadKey();
  76. }
  77. }
  78. }

Решение задачи: «Найти отрицательные элементы в строках, содержащих нулевой элемент и седловые точки матрицы»

textual
Листинг программы
  1.         static void Main(string[] args)
  2.         {
  3.             int rows = 5, cols = 7;
  4.             int min = -4, max = 3;
  5.             int[,] matrix = new int[rows, cols];
  6.  
  7.             Random rnd = new Random();
  8.             for (int j = 0; j < rows; j++)
  9.             {
  10.                 for (int i = 0; i < cols; i++)
  11.                 {
  12.                     matrix[j, i] = min + rnd.Next(max - min + 1);
  13.                     Console.Write("{0}\t", matrix[j, i]);
  14.                 }
  15.                 Console.WriteLine();
  16.             }
  17.  
  18.             Console.WriteLine();
  19.  
  20.             List<int>[] columnMaxesIndices = new List<int>[cols];
  21.             List<int>[] rowMinsIndices = new List<int>[rows];
  22.             for (int i = 0; i < cols; i++)
  23.                 columnMaxesIndices[i] = new List<int> { 0 };
  24.  
  25.             for (int j = 0; j < rows; j++)
  26.             {
  27.                 bool hasZero = false;
  28.                 int negativeCount = 0;
  29.                 rowMinsIndices[j] = new List<int> { 0 };
  30.  
  31.                 for (int i = 0; i < cols; i++)
  32.                 {
  33.                     if (matrix[j, i] < 0)
  34.                         negativeCount++;
  35.                     else if (matrix[j, i] == 0)
  36.                         hasZero = true;
  37.  
  38.                     int localMin = matrix[j, rowMinsIndices[j].First()];
  39.                     if (i == 0) ;
  40.                     else if (matrix[j, i] < localMin)
  41.                     {
  42.                         rowMinsIndices[j].Clear();
  43.                         rowMinsIndices[j].Add(i);
  44.                     }
  45.                     else if (matrix[j, i] == localMin)
  46.                         rowMinsIndices[j].Add(i);
  47.  
  48.                     int localMax = matrix[columnMaxesIndices[i].First(), i];
  49.                     if (j == 0) ;
  50.                     else if (matrix[j, i] > localMax)
  51.                     {
  52.                         columnMaxesIndices[i].Clear();
  53.                         columnMaxesIndices[i].Add(j);
  54.                     }
  55.                     else if (matrix[j, i] == localMax)
  56.                         columnMaxesIndices[i].Add(j);
  57.                 }
  58.  
  59.                 if (hasZero)
  60.                     Console.WriteLine("Line {0} contains zero elements and has {1} negative ones", j, negativeCount);
  61.                 else
  62.                     Console.WriteLine("Line {0} does not contain zero any element", j);
  63.             }
  64.  
  65.             Console.WriteLine("\nSaddle points:");
  66.             bool hasSaddle = false;
  67.             for (int j = 0; j < rows; j++)
  68.                 foreach (int i in rowMinsIndices[j])
  69.                     if (columnMaxesIndices[i].Contains(j))
  70.                     {
  71.                         Console.WriteLine("\trow: {0}; column: {1}", j + 1, i + 1);
  72.                         hasSaddle = true;
  73.                     }
  74.  
  75.             if (!hasSaddle)
  76.                 Console.WriteLine("No Saddle Points were found :(");
  77.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.2 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы