diff --git a/Lab2/Blue.cs b/Lab2/Blue.cs index 5f4d911..d99b4a8 100644 --- a/Lab2/Blue.cs +++ b/Lab2/Blue.cs @@ -1,158 +1,381 @@ -using System.Collections.Generic; -using System.ComponentModel; +using System.Linq; using System.Runtime.InteropServices; -namespace Lab2 +namespace Lab5 { public class Blue { - const double E = 0.0001; - public double Task1(int n, double x) + public double[] Task1(int[,] matrix) { - double answer = 0; + double[] answer = null; // code here - for (double i = 1; i <= n; i++) + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + answer = new double[n]; + for (int i = 0; i < n; i++) { - answer += Math.Sin(i * x) / Math.Pow(x, i - 1); + double S = 0, si = 0; + for (int j = 0; j < m; j++) + if (matrix[i, j] > 0) + { + S += matrix[i, j]; + si++; + } + if (si == 0) + answer[i] = 0; + else + answer[i] = S / si; } + // end return answer; } - public double Task2(int n) + public int[,] Task2(int[,] matrix) { - double answer = 0; + int[,] answer = null; // code here - double f = 1; - for (int i = 1; i <= n; i++) + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + int mxi = 0, mxj = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (matrix[i, j] > matrix[mxi, mxj]) + (mxi, mxj) = (i, j); + + answer = new int[n - 1, m - 1]; + if ((n == 0) || (m == 0)) + return answer; + + int ki = 0; + for (int i = 0; i < n; i++) { - f *= i; - answer += Math.Pow(-1, i) * Math.Pow(5, i) / f; + int kj = 0; + if (i == mxi) + continue; + + for (int j = 0; j < m; j++) + { + if (j == mxj) + continue; + answer[ki, kj] = matrix[i, j]; + kj++; + } + ki++; } + // end return answer; } - public long Task3(int n) + public void Task3(int[,] matrix) + { + + // code here + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + for (int i = 0; i < n; i++) + { + int mx = 0; + for (int j = 0; j < m; j++) + if (matrix[i, j] > matrix[i, mx]) + mx = j; + + for (int j = mx; j < (m - 1); j++) + (matrix[i, j], matrix[i, j + 1]) = (matrix[i, j + 1], matrix[i, j]); + } + + // end + + } + public int[,] Task4(int[,] matrix) { - long answer = 0; + int[,] answer = null; // code here - int f(int x) + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + answer = new int[n, m + 1]; + for (int i = 0; i < n; i++) { - if (x == 0 || x == 1) + int mx = 0; + for (int j = 0; j < m; j++) + if (matrix[i, j] > matrix[i, mx]) + mx = j; + + for (int j = 0; j < (m + 1); j++) { - return x; + if (j < (m - 1)) + answer[i, j] = matrix[i, j]; + else if (j == (m - 1)) + answer[i, j] = matrix[i, mx]; + else + answer[i, j] = matrix[i, (j - 1)]; } - return f(x - 1) + f(x - 2); } - for (int i = 0; i <= (n - 1); i++) + + // end + + return answer; + } + public int[] Task5(int[,] matrix) + { + int[] answer = null; + + // code here + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + int count = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if ((i + j) % 2 == 1) + count++; + + answer = new int[count]; + + count = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if ((i + j) % 2 == 1) + answer[count++] = matrix[i, j]; + + // end + + return answer; + } + public void Task6(int[,] matrix, int k) + { + + // code here + + if ((matrix.GetLength(0) != matrix.GetLength(1)) || (k >= matrix.GetLength(0))) + return; + + int n = Math.Min(matrix.GetLength(0), matrix.GetLength(1)); + + int mx = 0; + for (int i = 0; i < n; i++) + if (matrix[i, i] > matrix[mx, mx]) + mx = i; + + n = matrix.GetLength(0); int m = matrix.GetLength(1); + + int mn = 0; + bool flag = false; + for (int i = 0; i < n; i++) { - answer += f(i); + if ((!flag) && (matrix[i, k] < 0)) + { + mn = i; + flag = true; + } } + + if ((!flag) || (mx == mn)) + return; + + for (int j = 0; j < m; j++) + (matrix[mx, j], matrix[mn, j]) = (matrix[mn, j], matrix[mx, j]); + + // end + + } + public void Task7(int[,] matrix, int[] array) + { + + // code here + + int n = matrix.GetLength(0), m = matrix.GetLength(1), arrm = array.Length; + + if (m != arrm) + return; + + int mx = 0; + for (int i = 0; i < n; i++) + if (matrix[i, m - 2] > matrix[mx, m - 2]) + mx = i; + + for (int j = 0; j < m; j++) + (matrix[mx, j], array[j]) = (array[j], matrix[mx, j]); + // end - return answer; } - public int Task4(int a, int h, int L) + public void Task8(int[,] matrix) { - int answer = 0; // code here - int n = 1; - int s = 0; - while (s + a + (n - 1) * h <= L) + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + for (int j = 0; j < m; j++) { - s += a + (n - 1) * h; - n++; - answer++; + int mx = 0; + for (int i = 0; i < n; i++) + if (matrix[i, j] > matrix[mx, j]) + mx = i; + + if (mx >= (n / 2)) + continue; + + int s = 0; + for (int i = mx + 1; i < n; i++) + s += matrix[i, j]; + matrix[0, j] = s; } + // end - return answer; } - public double Task5(double x) + public void Task9(int[,] matrix) { - double answer = 0; // code here - double ch = 0, zn = 1; - double elem = ch / zn; - int i = 1; - ch += i; - zn *= x; - answer += elem; - elem = ch / zn; - i++; - while (elem > 0.0001) + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + for (int i = 0; i < (n - 1); i += 2) { - ch += i; - zn *= x; - answer += elem; - elem = ch / zn; - i++; + int mx1 = 0, mx2 = 0; + for (int j = 0; j < m; j++) + { + if (matrix[i, j] > matrix[i, mx1]) + mx1 = j; + if (matrix[i + 1, j] > matrix[i + 1, mx2]) + mx2 = j; + } + + (matrix[i, mx1], matrix[i + 1, mx2]) = (matrix[i + 1, mx2], matrix[i, mx1]); } + // end - return answer; } - public int Task6(int h, int S, int L) + public void Task10(int[,] matrix) { - int answer = 0; // code here - while (S < L) + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + if (n != m) + return; + + int mxi = 0; + for (int i = 0; i < n; i++) + if (matrix[i, i] > matrix[mxi, mxi]) + mxi = i; + + for (int i = 0; i < mxi; i++) { - S *= 2; - answer += h; + for (int j = 0; j < m; j++) + { + if (i < j) + { + matrix[i, j] = 0; + } + } } + // end - return answer; } - public (double a, int b, int c) Task7(double S, double I) + public void Task11(int[,] matrix) { - double a = 0; - int b = 0; - int c = 0; // code here - double q = 1 + I / 100; - a = (S * (Math.Pow(q, 7) - 1)) / (q - 1); - b = (int)(Math.Ceiling(Math.Log(100 * (q - 1) / S + 1, q))); - if (S <= 42) + + int n = matrix.GetLength(0), m = matrix.GetLength(1); + + int[] arrk = new int[n]; + int[] arri = new int[n]; + for (int i = 0; i < n; i++) { - c = (int)(Math.Floor(Math.Log(42 / S, q) + 2)) - 1; + int count = 0; + for (int j = 0; j < m; j++) + if (matrix[i, j] > 0) + count++; + arrk[i] = count; + arri[i] = i; } + + int jj = 0; + while (jj < n) + { + if ((jj == 0) || (arrk[jj] <= arrk[jj - 1])) + jj++; + else + { + (arrk[jj], arrk[jj - 1]) = (arrk[jj - 1], arrk[jj]); + (arri[jj], arri[jj - 1]) = (arri[jj - 1], arri[jj]); + jj--; + } + } + + int[,] ans = (int[,])matrix.Clone(); + + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + matrix[i, j] = ans[arri[i], j]; + // end - return (a, b, c); } - public (double SS, double SY) Task8(double a, double b, double h) + public int[][] Task12(int[][] array) { - double SS = 0; - double SY = 0; + int[][] answer = null; // code here - for (double x = a; x <= b; x += h) + + double s = 0; + int count = 0; + for (int i = 0; i < array.Length; i++) + { + for (int j = 0; j < array[i].Length; j++) + s += array[i][j]; + count += array[i].Length; + } + s /= count; + + count = 0; + for (int i = 0; i < array.Length; i++) + { + double s1 = 0; + for (int j = 0; j < array[i].Length; j++) + s1 += array[i][j]; + s1 /= array[i].Length; + + if (s1 >= s) + count++; + } + + answer = new int[count][]; + + count = 0; + for (int i = 0; i < array.Length; i++) { - int i = 0; - double f = 1; - x = Math.Round(x, 4); - while (Math.Abs((2 * i + 1) * Math.Pow(x, 2 * i) / f) >= 0.0001) + double s1 = 0; + for (int j = 0; j < array[i].Length; j++) + s1 += array[i][j]; + s1 /= array[i].Length; + + if (s1 >= s) { - SS += (2 * i + 1) * Math.Pow(x, 2 * i) / f; - i++; - f *= i; + answer[count] = new int[array[i].Length]; + Array.Copy(array[i], 0, answer[count++], 0, array[i].Length); } - SS += (2 * i + 1) * Math.Pow(x, 2 * i) / f; - SY += (1 + 2 * x * x) * Math.Exp(x * x); } + // end - return (SS, SY); + return answer; } + } -} +} \ No newline at end of file