Advertisement
RolexOsmiy

Untitled

Mar 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 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 laba_5
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Console.WriteLine("Введите номер задания");
  14. int z = Convert.ToInt32(Console.ReadLine());
  15. switch (z)
  16. {
  17. case (1):
  18. Console.WriteLine("Введите размерность");
  19. Task1.arr(Convert.ToInt32(Console.ReadLine()));
  20. break;
  21. case (2):
  22. Console.WriteLine("Введите размерность");
  23. Task2.sumArr(Convert.ToInt32(Console.ReadLine()));
  24. break;
  25. case (3):
  26. Console.WriteLine("Введите размерность");
  27. Task3.strArr(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()));
  28. break;
  29. case (4):
  30. Task4.matrix();
  31. break;
  32. }
  33. Console.ReadKey();
  34. }
  35. }
  36.  
  37. static class Task1
  38. {
  39. public static void arr(int q)
  40. {
  41. int[] myArr = new int[q];
  42. for (int i = 0; i < myArr.Length; i++)
  43. {
  44. Console.Write("Введите {0} элемент ", i + 1);
  45. myArr[i] = Int32.Parse(Console.ReadLine());
  46. }
  47. int p = 0;
  48. foreach (int n in myArr.SkipWhile(x => x != 0).Skip(1).TakeWhile(x => x != 0))
  49. p += n;
  50. Console.WriteLine(p);
  51. return;
  52. }
  53. }
  54. static class Task2
  55. {
  56. public static void sumArr(int q)
  57. {
  58. int sum = 0;
  59. int[] a = new int[q];
  60. for (int i = 0; i < q; i++)
  61. {
  62. Console.Write("Введите {0} элемент ", i + 1);
  63. a[i] = Int32.Parse(Console.ReadLine());
  64. }
  65. for (int i = q - 1; i >= 0; i--)
  66. {
  67. if (a[i] != 0) sum += a[i];
  68. else break;
  69. }
  70. Console.WriteLine("Сумма равна: " + sum);
  71. Console.ReadLine();
  72. }
  73. }
  74. static class Task3
  75. {
  76. public static void strArr(int n, int m)
  77. {
  78.  
  79. int strings = 0;
  80. int[,] myArr = new int[n, m];
  81. int[] s = new int[n];
  82. int i = 0, j = 0;
  83. Console.WriteLine("Заполни матрицу");
  84. for (i = 0; i < n; i++)
  85. for (i = 0; i < n; i++)
  86. {
  87. for (j = 0; j < m; j++)
  88. {
  89. Console.Write("mas[" + i + "," + j + "]: ");
  90.  
  91. myArr[i, j] = int.Parse(Console.ReadLine());
  92. }
  93. }
  94. Console.WriteLine();
  95. for (i = 0; i < n; i++)
  96. {
  97. for (j = 0; j < m; j++)
  98. {
  99. Console.Write(" myArr[" + i + "," + j + "]: " + myArr[i, j] + "\t");
  100. }
  101. Console.WriteLine();
  102. }
  103. Console.WriteLine("Строки в которых один нулевой элемент: ");
  104. for (i = 0; i < n; i++)
  105. {
  106. int k = 0;
  107. for (j = 0; j < m; j++)
  108. {
  109. if (myArr[i, j] == 0)
  110. {
  111. k++;
  112. strings += 1;
  113. }
  114. if (k == 1)
  115. {
  116. break;
  117. }
  118. }
  119. }
  120. Console.WriteLine("Сумма колличества строк = {0}",strings);
  121. }
  122. }
  123. static class Task4
  124. {
  125. public static void matrix()
  126. {
  127. Random rnd = new Random();
  128. int[,] matrix = new int[5, 5];
  129. for (int i = 0; i < matrix.GetLength(0); i++)
  130. {
  131. for (int j = 0; j < matrix.GetLength(1); j++)
  132. {
  133. Console.Write(matrix[i, j] = rnd.Next(10));
  134. Console.Write("\t");
  135. }
  136. Console.WriteLine();
  137. }
  138. int? max = null;
  139. for (int i = 1 - matrix.GetLength(1); i < matrix.GetLength(1); i++)
  140. {
  141. int sum = 0;
  142. int j = i < 0 ? -i : 0;
  143. int k = i < 0 ? 0 : i;
  144. while (j < matrix.GetLength(0) && k < matrix.GetLength(1))
  145. {
  146. sum += matrix[j++, k++];
  147. }
  148. Console.WriteLine(sum);
  149. if (max == null || sum > max) { max = sum; }
  150. }
  151. Console.WriteLine(max);
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement