Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MatrixPrint
- {
- static void Main()
- {
- Console.Write("Enter the size of the matrix: ");
- int n = int.Parse(Console.ReadLine());
- int[,] matrix = new int[n, n];
- int num = 1;
- Console.WriteLine();
- Console.WriteLine("First variant: ");
- for (int col = 0; col < n; col++)
- {
- for (int row = 0; row < n; row++)
- {
- matrix[row, col] = num;
- num++;
- }
- }
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- Console.Write("{0,3}", matrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- num = 1;
- Console.WriteLine("Second variant: ");
- for (int col = 0; col < n; col++)
- {
- if (col % 2 == 0)
- {
- for (int row = 0; row < n; row++)
- {
- matrix[row, col] = num;
- num++;
- }
- }
- else
- {
- for (int row = n - 1; row >= 0; row--)
- {
- matrix[row, col] = num;
- num++;
- }
- }
- }
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- Console.Write("{0,3}", matrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- num = 1;
- Console.WriteLine();
- Console.WriteLine("Third variant: ");
- for (int row = 0; row <= n - 1; row++)
- {
- for (int col = 0; col <= row; col++)
- {
- matrix[n - row + col - 1, col] = num;
- num++;
- }
- }
- for (int row = n - 2; row >= 0; row--)
- {
- for (int col = row; col >= 0; col--)
- {
- matrix[row - col, n - col - 1] = num;
- num++;
- }
- }
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- Console.Write("{0,3}", matrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- num = 1;
- int end=n;
- int start=0;
- Console.WriteLine();
- Console.WriteLine("Fourth variant:");
- do
- {
- for (int row = start; row < end; row++)
- {
- matrix[row, start] = num;
- num++;
- }
- for (int col = start + 1; col < end; col++)
- {
- matrix[end-1, col] = num;
- num++;
- }
- for (int row = end - 2; row >= start; row--)
- {
- matrix[row, end - 1] = num;
- num++;
- }
- for (int col = end - 2; col >= start + 1; col--)
- {
- matrix[start, col] = num;
- num++;
- }
- start++;
- end--;
- }
- while (end - start > 0);
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- Console.Write("{0,3}", matrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement