Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Exam
- {
- class Program
- {
- private static void InitMatrix(ref decimal[,] matrix)
- {
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- matrix[i, j] = i == j ? 0 : i > j ? i + j : 5 * i + (decimal) Math.Pow(j, 3);
- }
- }
- }
- private static decimal SumPositiveOnOdd(decimal[,] matrix)
- {
- decimal sum = 0;
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- sum += matrix[i, j] > 0 && (i % 2 != 0 || j % 2 != 0) ? matrix[i, j] : 0;
- }
- }
- return sum;
- }
- private static decimal SumNegativeOnAntiDiagonal(decimal[,] matrix)
- {
- decimal sum = 0;
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- sum += matrix[i, matrix.GetLength(1) - i] < 0 ? matrix[i, matrix.GetLength(1) - i] : 0;
- }
- return sum;
- }
- private static void SortMatrix(ref decimal[,] matrix)
- {
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- decimal[] array = new decimal[matrix.GetLength(1)];
- PopulateMatrix(ref matrix, ref array, i, true);
- SortArray(ref array, i % 2 == 0);
- PopulateMatrix(ref matrix, ref array, i, false);
- }
- }
- private static void PopulateMatrix(ref decimal[,] matrix, ref decimal[] array, int n, bool flag)
- {
- for (int i = 0; i < array.Length; i++)
- {
- if (flag)
- {
- array[i] = matrix[n, i];
- }
- else
- {
- matrix[n, i] = array[i];
- }
- }
- }
- private static void SortArray(ref decimal[] array, bool ascending)
- {
- bool change = false;
- for (int i = 0; i < array.Length - 1; i++)
- {
- if (ascending && array[i] > array[i + 1])
- {
- decimal temp = array[i];
- array[i] = array[i + 1];
- array[i + 1] = temp;
- change = true;
- }
- else if (!ascending && array[i] < array[i + 1])
- {
- decimal temp = array[i];
- array[i] = array[i + 1];
- array[i + 1] = temp;
- change = true;
- }
- }
- if (change)
- {
- SortArray(ref array, ascending);
- }
- }
- public static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- decimal[,] matrix = new decimal[n,n];
- InitMatrix(ref matrix);
- decimal sum1 = SumPositiveOnOdd(matrix);
- decimal sum2 = SumNegativeOnAntiDiagonal(matrix);
- SortMatrix(ref matrix);
- }
- }
- class Program2
- {
- private static int Iterative(int n)
- {
- if (n <= 3)
- {
- return n - n == 1 ? 2 : 0;
- }
- int n1 = -1, n2 = 2, n3 = 3, sum = 0;
- for (int i = 4; i <= n; i++)
- {
- sum = n3 + 5 * n2 - 7 * n1;
- n1 = n2;
- n2 = n3;
- n3 = sum;
- }
- return sum;
- }
- private static int Recursive(int n)
- {
- if (n <= 3)
- {
- return n - n == 1 ? 2 : 0;
- }
- return Recursive(n - 1) + 5 * Recursive(n - 2) - 7 * Recursive(n - 3);
- }
- public static void Main(string[] args)
- {
- int fifth = Iterative(5);
- int tenth = Recursive(10);
- }
- }
- }
Add Comment
Please, Sign In to add comment