Advertisement
FLISEN

Untitled

Jan 13th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2.  
  3.     class MatrixPrint
  4.     {
  5.         static void Main()
  6.         {
  7.             Console.Write("Enter the size of the matrix: ");
  8.             int n = int.Parse(Console.ReadLine());
  9.             int[,] matrix = new int[n, n];
  10.             int num = 1;
  11.  
  12.             Console.WriteLine();
  13.             Console.WriteLine("First variant: ");
  14.             for (int col = 0; col < n; col++)
  15.             {
  16.                 for (int row = 0; row < n; row++)
  17.                 {
  18.                     matrix[row, col] = num;
  19.                     num++;
  20.                 }
  21.             }
  22.  
  23.             for (int row = 0; row < n; row++)
  24.             {
  25.                 for (int col = 0; col < n; col++)
  26.                 {
  27.                     Console.Write("{0,3}", matrix[row, col]);
  28.                 }
  29.                 Console.WriteLine();
  30.             }
  31.             Console.WriteLine();
  32.  
  33.             num = 1;
  34.             Console.WriteLine("Second variant: ");
  35.             for (int col = 0; col < n; col++)
  36.             {
  37.                 if (col % 2 == 0)
  38.                 {
  39.                     for (int row = 0; row < n; row++)
  40.                     {
  41.                         matrix[row, col] = num;
  42.                         num++;
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     for (int row = n - 1; row >= 0; row--)
  48.                     {
  49.                         matrix[row, col] = num;
  50.                         num++;
  51.                     }
  52.                 }
  53.             }
  54.  
  55.             for (int row = 0; row < n; row++)
  56.             {
  57.                 for (int col = 0; col < n; col++)
  58.                 {
  59.                     Console.Write("{0,3}", matrix[row, col]);
  60.                 }
  61.                 Console.WriteLine();
  62.             }
  63.             Console.WriteLine();
  64.  
  65.             num = 1;
  66.             Console.WriteLine();
  67.             Console.WriteLine("Third variant: ");
  68.             for (int row = 0; row <= n - 1; row++)
  69.             {
  70.                 for (int col = 0; col <= row; col++)
  71.                 {
  72.                     matrix[n - row + col - 1, col] = num;
  73.                     num++;
  74.                 }
  75.             }
  76.  
  77.             for (int row = n - 2; row >= 0; row--)
  78.             {
  79.                 for (int col = row; col >= 0; col--)
  80.                 {
  81.                     matrix[row - col, n - col - 1] = num;
  82.                     num++;
  83.                 }
  84.             }
  85.  
  86.             for (int row = 0; row < n; row++)
  87.             {
  88.                 for (int col = 0; col < n; col++)
  89.                 {
  90.                     Console.Write("{0,3}", matrix[row, col]);
  91.                 }
  92.                 Console.WriteLine();
  93.             }
  94.             Console.WriteLine();
  95.  
  96.             num = 1;
  97.             int end=n;
  98.             int start=0;
  99.             Console.WriteLine();
  100.             Console.WriteLine("Fourth variant:");
  101.             do
  102.             {
  103.                 for (int row = start; row < end; row++)
  104.                 {
  105.                     matrix[row, start] = num;
  106.                     num++;
  107.                 }
  108.  
  109.                 for (int col = start + 1; col < end; col++)
  110.                 {
  111.                     matrix[end-1, col] = num;
  112.                     num++;
  113.                 }
  114.  
  115.                 for (int row = end - 2; row >= start; row--)
  116.                 {
  117.                     matrix[row, end - 1] = num;
  118.                     num++;
  119.                 }
  120.  
  121.                 for (int col = end - 2; col >= start + 1; col--)
  122.                 {
  123.                     matrix[start, col] = num;
  124.                     num++;
  125.                 }
  126.                 start++;
  127.                 end--;
  128.             }          
  129.             while (end - start > 0);
  130.  
  131.             for (int row = 0; row < n; row++)
  132.             {
  133.                 for (int col = 0; col < n; col++)
  134.                 {
  135.                     Console.Write("{0,3}", matrix[row, col]);
  136.                 }
  137.                 Console.WriteLine();
  138.             }
  139.             Console.WriteLine();
  140.         }
  141.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement