Last active
February 20, 2020 11:39
-
-
Save AviAvni/9d696a721e2a31d5145a8560f1f5f967 to your computer and use it in GitHub Desktop.
PrintSpiral
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace test | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| for (int i = 0; i < 10; i++) | |
| { | |
| PrintSpiral(i); | |
| } | |
| } | |
| private static void PrintSpiral(int n) | |
| { | |
| int[,] arr = new int[n, n]; | |
| int x = 0; | |
| int y = 0; | |
| int c = 1; | |
| double direction = 0; | |
| for (int t = 0; t < n - 1; t++) | |
| { | |
| for (int j = 0; j < Math.Max(3 - t, 2); j++) | |
| { | |
| for (int i = 0; i < n - t - 1; i++) | |
| { | |
| x += (int)Math.Cos(direction); | |
| y += (int)Math.Sin(direction); | |
| arr[y, x] = c++; | |
| } | |
| direction += Math.PI / 2; | |
| } | |
| } | |
| for (int i = 0; i < n; i++) | |
| { | |
| for (int j = 0; j < n; j++) | |
| { | |
| Console.Write(arr[i, j].ToString().PadLeft(c.ToString().Length) + " "); | |
| } | |
| Console.WriteLine(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment