Advertisement
Guest User

Untitled

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