fork download
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. int** generateSpiral(int);
  6. void printMatrix(int**, int, char*);
  7. void generateAndPrintSpiral(int);
  8.  
  9. int main() {
  10. generateAndPrintSpiral(5);
  11. generateAndPrintSpiral(6);
  12. generateAndPrintSpiral(12);
  13. return 0;
  14. }
  15.  
  16. int** generateSpiral(int n) {
  17. int** spiral = new int*[n];
  18. for (int i = 0; i < n; i++){
  19. spiral[i] = new int[n];
  20. }
  21.  
  22. // Defining the directions order and offsets: Right, Down, Left, Up
  23. int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  24.  
  25. //Adjusting the center for even inputs
  26. int center = n % 2 == 0 ? n / 2 - 1 : n / 2;
  27. int row, col, num = 1;
  28. row = col = center;
  29.  
  30. //In an expanding spiral pattern, the length of a segment is increased after drawing two segments
  31. int segmentsAfterToIncrease = 2;
  32. int segmentCounter = 0;
  33. int segmentLength = 1;
  34. int dir = 0;
  35.  
  36. //As soon as we're exceeding a boundary the spiral ends
  37. while (!(row >= n || row < 0 || col >= n || col < 0)) {
  38.  
  39. //Printing the numbers of the current segment
  40. for (int i = 0; i < segmentLength; i++) {
  41. spiral[row][col] = num++;
  42. row += directions[dir][0];
  43. col += directions[dir][1];
  44. }
  45.  
  46. //Rotating to the next direction
  47. dir = (dir + 1) % 4;
  48.  
  49. //Checking if we've printed the number of segments necessary to increase the length of the next segment
  50. segmentCounter = (segmentCounter + 1) % segmentsAfterToIncrease;
  51. if (segmentCounter == 0) {
  52. segmentLength++;
  53. }
  54. }
  55.  
  56. return spiral;
  57. }
  58.  
  59. void printMatrix(int** matrix, int n, char* format) {
  60. printf("\n");
  61. for (int i = 0; i < n; i++) {
  62. for (int j = 0; j < n; j++) {
  63. printf(format , matrix[i][j]);
  64. }
  65. printf("\n");
  66. }
  67. }
  68.  
  69. void generateAndPrintSpiral(int n){
  70. int** spiral = generateSpiral(n);
  71.  
  72. //Computing the number of digits of the highest number displayed in the spiral (the plus one is for an extra space)
  73. int numDigitsPlusSpace = ((int)ceil(log10(pow(n, 2))) + 1);
  74.  
  75. //Converting the previous integer to a string (this is necessary only to create a dynamic format string)
  76. // - Defining the number's length as a string
  77. // - Creating an array to contain the string
  78. // - Converting the number into a string
  79. int numDigitsTemp = (int)ceil(log10(numDigitsPlusSpace));
  80. char* numDigitsTempStr = new char[numDigitsTemp];
  81. sprintf(numDigitsTempStr, "%d", numDigitsPlusSpace);
  82.  
  83. //Creating the format for each number (for example: "%2d ", "%5d ", etc.)
  84. char* format = new char[numDigitsTemp + 3];
  85. strcpy(format, "%");
  86. strcat(format, numDigitsTempStr);
  87. strcat(format, "d ");
  88.  
  89. printMatrix(spiral, n, format);
  90. delete[] spiral;
  91. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
 21  22  23  24  25 
 20   7   8   9  10 
 19   6   1   2  11 
 18   5   4   3  12 
 17  16  15  14  13 

 21  22  23  24  25  26 
 20   7   8   9  10  27 
 19   6   1   2  11  28 
 18   5   4   3  12  29 
 17  16  15  14  13  30 
 36  35  34  33  32  31 

 111  112  113  114  115  116  117  118  119  120  121  122 
 110   73   74   75   76   77   78   79   80   81   82  123 
 109   72   43   44   45   46   47   48   49   50   83  124 
 108   71   42   21   22   23   24   25   26   51   84  125 
 107   70   41   20    7    8    9   10   27   52   85  126 
 106   69   40   19    6    1    2   11   28   53   86  127 
 105   68   39   18    5    4    3   12   29   54   87  128 
 104   67   38   17   16   15   14   13   30   55   88  129 
 103   66   37   36   35   34   33   32   31   56   89  130 
 102   65   64   63   62   61   60   59   58   57   90  131 
 101  100   99   98   97   96   95   94   93   92   91  132 
 144  143  142  141  140  139  138  137  136  135  134  133