Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Sphirala
- {
- static void Main()
- {
- int colStart, colEnd, oldColEnd;
- int rowStart, rowEnd;
- int rowFix,colFix;
- int col, row;
- int rowIndex, colIndex;
- int num = 1;
- int directionChange = 1;
- int n = int.Parse(Console.ReadLine());
- int[,] matrix = new int[n,n];
- for (int i = 0; i <= n - 1; i++)
- {
- matrix[0, i] = num;
- num++;
- }
- rowStart = rowIndex = 1;
- rowEnd = n - 1;
- colStart = colIndex = n - 2;
- colEnd = 0;
- rowFix = n - 1;
- colFix = n - 1;
- directionChange = 1;
- while (directionChange <= n-1)
- {
- if (directionChange % 2 != 0) // down & left
- {
- for (col = colFix, row = rowStart; row <= rowEnd; row++)
- {
- matrix[row,col] = num;
- num++;
- }
- for (row = rowFix, col = colStart; col >= colEnd; col--)
- {
- matrix[row,col] = num;
- num++;
- }
- rowEnd = rowStart;
- rowFix = rowStart;
- rowStart = colStart; // new rowStart
- oldColEnd = colEnd;
- colEnd = colStart;
- colFix = oldColEnd; // old colEnd
- colStart = colFix + 1; // new colFix
- }
- else // up & right
- {
- {
- for (col = colFix, row = rowStart; row >= rowEnd; row--)
- {
- matrix[row, col] = num;
- num++;
- }
- rowFix = rowEnd;
- for (row = rowFix, col = colStart; col <= colEnd; col++)
- {
- matrix[row, col] = num;
- num++;
- }
- colIndex--;
- rowIndex++;
- colFix = colEnd; //old colEnd
- rowFix = rowStart;
- colEnd = colStart; //old colStar
- colStart = colIndex;
- rowEnd = rowStart;
- rowStart = rowIndex;
- }
- }
- directionChange++;
- }
- for (int rows = 0; rows < matrix.GetLength(0); rows++)
- {
- for (int cols = 0; cols < matrix.GetLength(1); cols++)
- {
- string output = " "+ matrix[rows, cols];
- if (output.Length == 2)
- {
- output = output + " ";
- }
- Console.Write(output);
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement