using System; class Program { static void Main(string[] args) { int[,] matrix = { { 1, 2, 3, 4 }, { 5, 6, -7, 8 }, { 9, 10, 11, -12 }, { 13, -14, 15, 16 } }; int columnsWithoutNegativesSum = 0; for (int j = 0; j < matrix.GetLength(1); j++) { bool hasNegative = false; int columnSum = 0; for (int i = 0; i < matrix.GetLength(0); i++) { if (matrix[i, j] < 0) { hasNegative = true; break; } columnSum += matrix[i, j]; } if (!hasNegative) { columnsWithoutNegativesSum += columnSum; } } Console.WriteLine("Sum of elements in columns without negatives: {0}", columnsWithoutNegativesSum); int diagonalsSumMin = int.MaxValue; for (int i = matrix.GetLength(0) - 1; i >= 0; i--) { int diagonalSum = 0; for (int j = 0; i + j < matrix.GetLength(0) && j < matrix.GetLength(1); j++) { diagonalSum += Math.Abs(matrix[i + j, j]); } diagonalsSumMin = Math.Min(diagonalsSumMin, diagonalSum); } for (int j = 1; j < matrix.GetLength(1); j++) { int diagonalSum = 0; for (int i = 0; i < matrix.GetLength(0) && j + i < matrix.GetLength(1); i++) { diagonalSum += Math.Abs(matrix[i, j + i]); } diagonalsSumMin = Math.Min(diagonalsSumMin, diagonalSum); } Console.WriteLine("Min of sums of absolute values of elements on diagonals parallel to the secondary diagonal: {0}", diagonalsSumMin); Console.ReadKey(); } } CSHARP using System; class Program { static void Main(string[] args) { int[,] matrix = { { 1, 2, 3, 4 }, { 5, 6, -7, 8 }, { 9, 10, 11, -12 }, { 13, -14, 15, 16 } }; int columnsWithoutNegativesSum = 0; for (int j = 0; j < matrix.GetLength(1); j++) { bool hasNegative = false; int columnSum = 0; for (int i = 0; i < matrix.GetLength(0); i++) { if (matrix[i, j] < 0) { hasNegative = true; break; } columnSum += matrix[i, j]; } if (!hasNegative) { columnsWithoutNegativesSum += columnSum; } } Console.WriteLine("Sum of elements in columns without negatives: {0}", columnsWithoutNegativesSum); int diagonalsSumMin = int.MaxValue; for (int i = matrix.GetLength(0) - 1; i >= 0; i--) { int diagonalSum = 0; for (int j = 0; i + j < matrix.GetLength(0) && j < matrix.GetLength(1); j++) { diagonalSum += Math.Abs(matrix[i + j, j]); } diagonalsSumMin = Math.Min(diagonalsSumMin, diagonalSum); } for (int j = 1; j < matrix.GetLength(1); j++) { int diagonalSum = 0; for (int i = 0; i < matrix.GetLength(0) && j + i < matrix.GetLength(1); i++) { diagonalSum += Math.Abs(matrix[i, j + i]); } diagonalsSumMin = Math.Min(diagonalsSumMin, diagonalSum); } Console.WriteLine("Min of sums of absolute values of elements on diagonals parallel to the secondary diagonal: {0}", diagonalsSumMin); Console.ReadKey(); } }
Додик, привет! Я могу помочь с генерацией матрицы. Какие параметры должны быть у матрицы? Размерность? Диапазон значений? Тип элементов?