Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Exercise14Spiral
- {
- static void Main()
- {
- int input = int.Parse(Console.ReadLine());
- int[,] matrix = new int[input, input];
- int x = input - 1;
- int y = 0;
- int direction = 2;
- int number = input;
- for (int a = input; a > 0; a--)
- {
- matrix[0, input - a] = (input - a) + 1;
- for (int b = 0; b < 2; b++)
- {
- for (int c = 0; c < a - 1; c++)
- {
- switch (direction)
- {
- case 1: x++; break;
- case 2: y++; break;
- case 3: x--; break;
- case 4: y--; break;
- }
- number++;
- matrix[y, x] = number;
- }
- direction++;
- if (direction > 4)
- {
- direction = 1;
- }
- }
- }
- for (int matrixY = 0; matrixY < input; matrixY++)
- {
- for (int matrixX = 0; matrixX < input; matrixX++)
- {
- Console.Write(Convert.ToString(matrix[matrixY, matrixX]).PadLeft((Convert.ToString(input * input).Length + 1), ' '));
- }
- Console.WriteLine(Environment.NewLine);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement