Guest User

Untitled

a guest
Jan 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Exam
  4. {
  5. class Program
  6. {
  7. private static void InitMatrix(ref decimal[,] matrix)
  8. {
  9. for (int i = 0; i < matrix.GetLength(0); i++)
  10. {
  11. for (int j = 0; j < matrix.GetLength(1); j++)
  12. {
  13. matrix[i, j] = i == j ? 0 : i > j ? i + j : 5 * i + (decimal) Math.Pow(j, 3);
  14. }
  15. }
  16. }
  17.  
  18. private static decimal SumPositiveOnOdd(decimal[,] matrix)
  19. {
  20. decimal sum = 0;
  21. for (int i = 0; i < matrix.GetLength(0); i++)
  22. {
  23. for (int j = 0; j < matrix.GetLength(1); j++)
  24. {
  25. sum += matrix[i, j] > 0 && (i % 2 != 0 || j % 2 != 0) ? matrix[i, j] : 0;
  26. }
  27. }
  28. return sum;
  29. }
  30.  
  31. private static decimal SumNegativeOnAntiDiagonal(decimal[,] matrix)
  32. {
  33. decimal sum = 0;
  34. for (int i = 0; i < matrix.GetLength(0); i++)
  35. {
  36. sum += matrix[i, matrix.GetLength(1) - i] < 0 ? matrix[i, matrix.GetLength(1) - i] : 0;
  37. }
  38. return sum;
  39. }
  40.  
  41. private static void SortMatrix(ref decimal[,] matrix)
  42. {
  43. for (int i = 0; i < matrix.GetLength(0); i++)
  44. {
  45. decimal[] array = new decimal[matrix.GetLength(1)];
  46. PopulateMatrix(ref matrix, ref array, i, true);
  47. SortArray(ref array, i % 2 == 0);
  48. PopulateMatrix(ref matrix, ref array, i, false);
  49. }
  50. }
  51.  
  52. private static void PopulateMatrix(ref decimal[,] matrix, ref decimal[] array, int n, bool flag)
  53. {
  54. for (int i = 0; i < array.Length; i++)
  55. {
  56. if (flag)
  57. {
  58. array[i] = matrix[n, i];
  59. }
  60. else
  61. {
  62. matrix[n, i] = array[i];
  63. }
  64. }
  65. }
  66.  
  67. private static void SortArray(ref decimal[] array, bool ascending)
  68. {
  69. bool change = false;
  70. for (int i = 0; i < array.Length - 1; i++)
  71. {
  72. if (ascending && array[i] > array[i + 1])
  73. {
  74. decimal temp = array[i];
  75. array[i] = array[i + 1];
  76. array[i + 1] = temp;
  77. change = true;
  78. }
  79. else if (!ascending && array[i] < array[i + 1])
  80. {
  81. decimal temp = array[i];
  82. array[i] = array[i + 1];
  83. array[i + 1] = temp;
  84. change = true;
  85. }
  86. }
  87. if (change)
  88. {
  89. SortArray(ref array, ascending);
  90. }
  91. }
  92.  
  93. public static void Main(string[] args)
  94. {
  95. int n = int.Parse(Console.ReadLine());
  96. decimal[,] matrix = new decimal[n,n];
  97. InitMatrix(ref matrix);
  98. decimal sum1 = SumPositiveOnOdd(matrix);
  99. decimal sum2 = SumNegativeOnAntiDiagonal(matrix);
  100. SortMatrix(ref matrix);
  101. }
  102.  
  103. }
  104.  
  105. class Program2
  106. {
  107. private static int Iterative(int n)
  108. {
  109. if (n <= 3)
  110. {
  111. return n - n == 1 ? 2 : 0;
  112. }
  113. int n1 = -1, n2 = 2, n3 = 3, sum = 0;
  114. for (int i = 4; i <= n; i++)
  115. {
  116. sum = n3 + 5 * n2 - 7 * n1;
  117. n1 = n2;
  118. n2 = n3;
  119. n3 = sum;
  120. }
  121. return sum;
  122. }
  123.  
  124. private static int Recursive(int n)
  125. {
  126. if (n <= 3)
  127. {
  128. return n - n == 1 ? 2 : 0;
  129. }
  130. return Recursive(n - 1) + 5 * Recursive(n - 2) - 7 * Recursive(n - 3);
  131. }
  132.  
  133. public static void Main(string[] args)
  134. {
  135. int fifth = Iterative(5);
  136. int tenth = Recursive(10);
  137. }
  138. }
  139. }
Add Comment
Please, Sign In to add comment