Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Spiral_Matrix
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14.  
  15. int[,] matrx = new int[n, n];
  16.  
  17. int rows = 0;
  18. int collumns = 0;
  19. string move = "right";
  20.  
  21. int maxNumber = n * n;
  22.  
  23. for (int i = 1; i <= maxNumber; i++)
  24. {
  25. if (move == "right" && (collumns > n - 1 || matrx[rows, collumns] != 0))
  26. {
  27. move = "down";
  28. collumns--;
  29. rows++;
  30. }
  31. if (move == "down" && (rows > n - 1 || matrx[rows, collumns] != 0))
  32. {
  33. rows--;
  34. collumns--;
  35. move = "left";
  36. }
  37. if (move == "left" && (collumns < 0 || matrx[rows, collumns] != 0))
  38. {
  39. move = "up";
  40. collumns++;
  41. rows--;
  42. }
  43.  
  44. if (move == "up" && (rows < 0 || matrx[rows, collumns] != 0))
  45. {
  46. move = "right";
  47. rows++;
  48. collumns++;
  49.  
  50. }
  51. matrx[rows, collumns] = i;
  52. if (move == "right")
  53. {
  54. collumns++;
  55. }
  56. if (move == "down")
  57. {
  58. rows++;
  59. }
  60. if (move == "left")
  61. {
  62. collumns--;
  63. }
  64. if (move == "up")
  65. {
  66. rows--;
  67. }
  68.  
  69.  
  70. }
  71.  
  72. for (int x1 = 0; x1 < n; x1++)
  73. {
  74. for (int x2 = 0; x2 < n; x2++)
  75. {
  76. Console.Write("{0,4}", matrx[x1, x2] );
  77. }
  78. Console.WriteLine();
  79. }
  80. Console.WriteLine();
  81.  
  82.  
  83.  
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement