Дана целочисленная прямоугольная матрица. Определить номер первого из столбцов, содержащих хотя бы один нулевой элемент - C#
Формулировка задачи:
Характеристикой строки целочисленной матрицы назовем сумму ее отрицательных четных элементов.Переставляя строки заданной матрицы, расположить их в соответствии с убыванием характеристик.
Вот что у меня получилось.Не могу понять как сделать сортировку строк
помогите плиз!
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main() { const int n = 4; int m = 0; var a = new Random();// неопределенный тип переменной int[,] array = new int[n, n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) array[i, j] = a.Next(-10, 10); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Write("{0} ", array[i, j]); Console.WriteLine(); } Console.WriteLine(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (array[i, j] == 0) { m = ++j; break; } Console.WriteLine("выввод первого столба с 0 {0}", m); int sum1 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) if (((array[i, j]) <= 0) && (j + 1) % 2 == 0) sum1 += array[i, j]; break; } Console.WriteLine("cymma {0}", sum1); int sum2 = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < n; j++) if (((array[i, j]) <= 0) && (j + 1) % 2 == 0) sum2 += array[i, j]; break; } Console.WriteLine("cymma {0}", sum2); int sum3 = 0; for (int i = 2; i < n; i++) { for (int j = 0; j < n; j++) if (((array[i, j]) <= 0) && (j + 1) % 2 == 0) sum3 += array[i, j]; break; } Console.WriteLine("cymma {0}", sum3); int sum4 = 0; for (int i = 3; i < n; i++) { for (int j = 0; j < n; j++) if (((array[i, j]) <= 0) && (j + 1) % 2 == 0) sum4 += array[i, j]; Console.WriteLine("cymma {0}", sum4); } int[] arr = { sum1, sum2, sum3, sum4 };//сортировка Array.Sort(arr); for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { sum1 = array[i, j]; sum2 = array[1, j]; sum3 = array[2, j]; sum4 = array[3, j]; Console.WriteLine("массив{0}", sum3, sum2, sum1, sum4); Console.ReadKey(); } } } }
Решение задачи: «Дана целочисленная прямоугольная матрица. Определить номер первого из столбцов, содержащих хотя бы один нулевой элемент»
textual
Листинг программы
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { struct pair { public int summa; public int index; static void Main() { const int n = 4; pair[] ar = new pair[n]; int m = 0; var a = new Random();// неопределенный тип переменной int[,] array = new int[n, n]; int k = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) array[i, j] = a.Next(-10, 10); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Write("{0} ", array[i, j]); Console.WriteLine(); } Console.WriteLine(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (array[i, j] == 0) { m = ++j; break; } Console.WriteLine("Номер первого столбца содержащего '0' = {0}", m); int sum1 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) if (((array[i, j]) <= 0) && (j + 1) % 2 == 0) sum1 += array[i, j]; ar[k].summa = sum1; ar[k].index = i; sum1 = 0; ++k; } for (int i = 0; i < n; i++) Console.WriteLine("cymma {0}", ar[i].summa); Console.WriteLine(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) if (ar[i].summa > ar[j].summa) { int tempsum = ar[i].summa; ar[i].summa = ar[j].summa; ar[j].summa = tempsum; int tempindex = ar[i].index; ar[i].index = ar[j].index; ar[j].index = tempindex; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Write(" {0}", array[ar[i].index, j]); Console.WriteLine(); } Console.ReadKey(); } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д