Advertisement
Guest User

Solution

a guest
Nov 14th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1.  
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Rextester
  7. {
  8.     public class Program
  9.     {
  10. public static void Sort(int[,] arr)
  11.  {
  12.  
  13. for (int i = 0; i < arr.GetLength(0); i++)
  14. {
  15.       for (int j = arr.GetLength(1) - 1; j > 0; j--)
  16.      {
  17.           for (int k = 0; k < j; k++) {
  18.             if (arr[i, k] > arr[i, k + 1]) {
  19.             int myTemp = arr[i, k];
  20.             arr[i, k] = arr[i, k + 1];  
  21.             arr[i, k + 1] = myTemp;
  22.        }
  23.      }
  24.    }
  25.  }
  26. }
  27.  
  28. public static List<int> GetSolution(int[,] P)
  29. {        
  30.         var Q = new int[P.GetLength(0), P.GetLength(1)];
  31.        
  32.         for (int i = 0; i < P.GetLength(0); i += 2)
  33.         {
  34.             for (int j = 0; j < P.GetLength(1); j++)
  35.             {
  36.                 Q[i,j] = P[i,j];
  37.             }
  38.         }
  39.  
  40.         Sort(Q);
  41.        
  42.         var arr = new int[Q.GetLength(0) * Q.GetLength(1)];
  43.         int arrIndex = 0;
  44.        
  45.         for (int i = 0; i < Q.GetLength(0); i++)
  46.         {
  47.             for (int j = 0; j < Q.GetLength(1); j++)
  48.             {
  49.                 arr[arrIndex] = Q[i,j];
  50.                 arrIndex++;
  51.             }
  52.         }
  53.        
  54.     var list = new List<int>();
  55.    
  56.         for (int i = 0; i < arr.Length; i++)
  57.         {
  58.             if (arr[i] == i)
  59.             {
  60.                 list.Add(i);
  61.             }
  62.         }
  63.    
  64.     return list;
  65. }        
  66.        
  67.     public static void Main(String[] args) {
  68.         var P = new int[,]
  69.         {
  70.             {
  71.                 1,1,1,2
  72.             },
  73.             {
  74.                 2,3,3,4
  75.             },
  76.             {
  77.                 4,4,4,4
  78.             },
  79.             {
  80.                 1,2,3,1
  81.             }
  82.         };
  83.        
  84.         var result = GetSolution(P);
  85.         var resStr = String.Join(", ", result);
  86.        
  87.         Console.WriteLine(resStr);
  88.     }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement