Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp6
  8. {
  9. class Matr
  10. {
  11. public static void InSertMatr(int n, int[,] a)
  12. {
  13. Random rand = new Random();
  14. for (int i = 0; i < n; i++)
  15. {
  16. for (int j = 0; j < n; j++)
  17. {
  18. a[i, j] = rand.Next(-50, 50);
  19. }
  20. }
  21. }
  22. public static void OutPutMatr(int n, int[,] a)
  23. {
  24. Console.WriteLine("Сгенерированная матрица: ");
  25. for (int i = 0; i < n; i++)
  26. {
  27. for (int j = 0; j < n; j++)
  28. Console.Write("{0, 5}", a[i, j]);
  29. Console.WriteLine();
  30. }
  31. }
  32.  
  33. public static int Sum(int[,] a, int n)
  34. {
  35. int summ = 0;
  36.  
  37. for (int j = 0; j < n; j++)
  38. {
  39. int sumj = 0;
  40.  
  41. for (int i = 0; i < n; i++)
  42. {
  43.  
  44. while (a[i, j] >= 0 && i < n)
  45. {
  46. sumj += a[i, j];
  47. if (sumj < 0)
  48. { sumj = 0; }
  49. }
  50. }
  51. summ += sumj;
  52. }
  53. return summ;
  54. }
  55.  
  56. public static int Min(int [,] a, int n)
  57. {
  58. int minSum = 32000, b, c;
  59. for (int i = 1; i < n - 1; i++)
  60. {
  61. b = 0;
  62. c = 1000;
  63. for (int j = 0; j < n - i; j++)
  64. {
  65. b += Math.Abs(a[j + i, j]);
  66. c += Math.Abs(a[j, j + i]);
  67. }
  68. if (b< minSum || c < minSum) minSum = (b < c) ? b : c;
  69. }
  70. return minSum;
  71. }
  72.  
  73. }
  74. }
  75. using System;
  76. using System.Collections.Generic;
  77. using System.Linq;
  78. using System.Text;
  79. using System.Threading.Tasks;
  80.  
  81. namespace ConsoleApp6
  82. {
  83. class Program
  84. {
  85. static void Main(string[] args)
  86. {
  87. string str;
  88. int n;
  89. Console.WriteLine("Введите число строк и столбцов");
  90. str = Console.ReadLine();
  91. while (!int.TryParse(str, out n))
  92. {
  93. Console.WriteLine("Неправильный формат ввода");
  94. Console.WriteLine("Введите число элементов ");
  95.  
  96. str = Console.ReadLine();
  97. }
  98. /*int m;
  99. Console.WriteLine("Введите число столбцов");
  100. str = Console.ReadLine();
  101. while (!int.TryParse(str, out m)&&m!=n)
  102. {
  103. Console.WriteLine("Неправильный формат ввода");
  104. Console.WriteLine("Введите ЧИСЛО элементов ");
  105.  
  106. str = Console.ReadLine();
  107. }*/
  108. int[,] a = new int[n, n];
  109. Matr.InSertMatr(n, a);
  110. Matr.OutPutMatr(n, a);
  111. Console.WriteLine();
  112. //if (Matr.Seach(a,n)>0)
  113. Console.WriteLine("Сумма элементов столбцов,не содержащих отрицательных элементов:", Matr.Sum(a, n));
  114. Console.WriteLine();
  115. Console.WriteLine("Минимум среди сумм модулей элементов диагоналей, параллельных побочной диагонали матрицы:", Matr.Min(a, n));
  116.  
  117.  
  118.  
  119.  
  120. Console.ReadKey();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement