Advertisement
striking

Untitled

Feb 6th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. class SpiralMatrix
  3. {
  4. static void Main(string[] args)
  5. {
  6. int n = int.Parse(Console.ReadLine());
  7. int[,] spiralMatrix = new int[n, n];
  8.  
  9. int changerRight = 1;
  10. int changerDown = 1;
  11. int changerLeft = 0;
  12. int changerUp = 1;
  13. int rowRight = 0;
  14. int colRIght = 0;
  15. int movingNumber = 1;
  16. int rowDown = 1;
  17. int rowLeft = n - 1;
  18. int colLeft = n - 2;
  19. int rowUp = n - 2;
  20.  
  21. bool whenToStop = true;
  22.  
  23. do
  24. {
  25. whenToStop = false;
  26. while (colRIght < (n - changerRight) + 1)
  27. {
  28. spiralMatrix[rowRight, colRIght] = movingNumber;
  29. movingNumber++;
  30. colRIght++;
  31. whenToStop = true;
  32. }
  33. colRIght = changerRight;
  34. rowRight = changerRight;
  35. changerRight++;
  36.  
  37. while (rowDown <= n - changerDown)
  38. {
  39. spiralMatrix[rowDown, n - changerDown] = movingNumber;
  40. movingNumber++;
  41. rowDown++;
  42. whenToStop = true;
  43. }
  44. changerDown++;
  45. rowDown = changerDown;
  46.  
  47. while (colLeft >= n - (n - changerLeft))
  48. {
  49. spiralMatrix[rowLeft - changerLeft, colLeft] = movingNumber;
  50. movingNumber++;
  51. colLeft--;
  52. whenToStop = true;
  53. }
  54. changerLeft++;
  55. colLeft = (n - 2) - changerLeft;
  56.  
  57. while (rowUp >= n - (n - changerUp))
  58. {
  59. spiralMatrix[rowUp, 0 + (changerUp - 1)] = movingNumber;
  60. movingNumber++;
  61. rowUp--;
  62. whenToStop = true;
  63. }
  64. rowUp = (n - 2) - changerUp;
  65. changerUp++;
  66.  
  67. } while (whenToStop);
  68.  
  69. for (int i = 0; i < n; i++)
  70. {
  71. for (int p = 0; p < n; p++)
  72. {
  73. Console.Write("{0, 6}", spiralMatrix[i, p]);
  74. }
  75. Console.WriteLine();
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement