Advertisement
j0nze

Fill Matrix

Jul 11th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             string type = Console.ReadLine();
  11.  
  12.             int[,] matrix = new int[n, n];
  13.  
  14.             if (type == "a")
  15.             {
  16.                 FillMatrixCaseA(matrix, n);
  17.             }
  18.             else if (type == "b")
  19.             {
  20.                 FillMatrixCaseB(matrix, n);
  21.             }
  22.             else if (type == "c")
  23.             {
  24.                 FillMatrixCaseC(matrix, n);
  25.             }
  26.             else if (type == "d")
  27.             {
  28.                 FillMatrixCaseD(matrix, n);
  29.             }
  30.  
  31.             PrintMatrix(matrix);
  32.         }
  33.  
  34.         private static void PrintMatrix(int[,] matrix)
  35.         {
  36.             for (int i = 0; i < matrix.GetLength(0); i++)
  37.             {
  38.                 for (int j = 0; j < matrix.GetLength(0); j++)
  39.                 {
  40.                     Console.Write(matrix[i,j] + " ");
  41.                 }
  42.                 Console.WriteLine();
  43.             }
  44.         }
  45.  
  46.         private static void FillMatrixCaseD(int[,] matrix, int n)
  47.         {
  48.             int step = 1;
  49.             int row = 0;
  50.             int column = 0;
  51.            
  52.             while (row < matrix.GetLength(0))
  53.             {
  54.                 matrix[row, column] = step;
  55.                 row++;
  56.                 step++;
  57.             }
  58.             row--;
  59.             column++;
  60.             while (column < matrix.GetLength(1))
  61.             {
  62.                 matrix[row, column] = step;
  63.                 column++;
  64.                 step++;
  65.             }
  66.             row--;
  67.             column--;
  68.             while (row >= 0)
  69.             {
  70.                 matrix[row, column] = step;
  71.                 step++;
  72.                 row--;
  73.             }
  74.             column--;
  75.             row++;
  76.             while (column >= 1)
  77.             {
  78.                 matrix[row, column] = step;
  79.                 step++;
  80.                 column--;
  81.             }
  82.             column++;
  83.             row++;
  84.  
  85.             while (step <= matrix.Length)
  86.             {
  87.                 while (matrix[row, column] == 0)
  88.                 {
  89.                     matrix[row, column] = step;
  90.                     row++;
  91.                     step++;
  92.                 }
  93.                 if (step > matrix.Length)
  94.                 {
  95.                     break;
  96.                 }
  97.                 row--;
  98.                 column++;
  99.                 while (matrix[row, column] == 0)
  100.                 {
  101.                     matrix[row, column] = step;
  102.                     column++;
  103.                     step++;
  104.                 }
  105.                 if (step > matrix.Length)
  106.                 {
  107.                     break;
  108.                 }
  109.                 row--;
  110.                 column--;
  111.                 while (matrix[row, column] == 0)
  112.                 {
  113.                     matrix[row, column] = step;
  114.                     step++;
  115.                     row--;
  116.                 }
  117.                 if (step > matrix.Length)
  118.                 {
  119.                     break;
  120.                 }
  121.                 column--;
  122.                 row++;
  123.                 while (matrix[row, column] == 0)
  124.                 {
  125.                     matrix[row, column] = step;
  126.                     step++;
  127.                     column--;
  128.                 }
  129.                 if (step > matrix.Length)
  130.                 {
  131.                     break;
  132.                 }
  133.                 column++;
  134.                 row++;
  135.             }
  136.  
  137.            
  138.         }
  139.  
  140.         private static void FillMatrixCaseC(int[,] matrix, int n)
  141.         {
  142.             int row = n - 1;
  143.             int col = 0;
  144.             int count = 1;
  145.             int num = 1;
  146.             for (int i = n - 1; i >= 0; i--)
  147.             {
  148.                 for (int j = 0; j < count; j++)
  149.                 {
  150.                     matrix[row, col] = num;
  151.                     row++;
  152.                     col++;
  153.                     num++;
  154.                 }
  155.                 row = i - 1;
  156.                 col = 0;
  157.                 count++;
  158.             }
  159.             row = 0;
  160.             count = 0;
  161.             for (int i = 0; i < n - 1; i++)
  162.             {
  163.                 col = i + 1;
  164.                 for (int j = count; j < n - 1; j++)
  165.                 {
  166.                     matrix[row, col] = num;
  167.                     num++;
  168.                     row++;
  169.                     col++;
  170.                 }
  171.                 row = 0;
  172.                 count++;
  173.             }
  174.         }
  175.  
  176.         private static void FillMatrixCaseB(int[,] matrix, int n)
  177.         {
  178.             int count = 1;
  179.             int row = 0;
  180.             while (count <= n * n)
  181.             {
  182.                 for (int i = row; i < row + 1; i++)
  183.                 {
  184.                     for (int j = 0; j < n; j++)
  185.                     {
  186.                         matrix[j, i] = count;
  187.                         count++;
  188.                     }
  189.                 }
  190.                 row++;
  191.                 if (count >= n * n)
  192.                 {
  193.                     break;
  194.                 }
  195.                 for (int i = row; i < row + 1; i++)
  196.                 {
  197.                     for (int j = n - 1; j >= 0; j--)
  198.                     {
  199.                         matrix[j, i] = count;
  200.                         count++;
  201.                     }
  202.                 }
  203.                 row++;
  204.             }
  205.         }
  206.  
  207.         private static void FillMatrixCaseA(int[,] matrix, int n)
  208.         {
  209.             int num = 1;
  210.             for (int i = 0; i < n; i++)
  211.             {
  212.                 for (int j = 0; j < n; j++)
  213.                 {
  214.                     matrix[j, i] = num;
  215.                     num++;
  216.                 }
  217.             }
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement