fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int find_digits(int num) {
  6. char buf[128];
  7. sprintf(buf, "%d", num * num);
  8. return strlen(buf);
  9. }
  10.  
  11. void print(int num, int m[num][num]) {
  12. unsigned int i, j;
  13. int padding = find_digits(num);
  14. for (i = 0; i < num; i++) {
  15. for (j = 0; j < num; j++)
  16. printf("%*d ", padding, m[j][i]);
  17. printf("\n");
  18. }
  19. }
  20.  
  21. void spiral(unsigned int num) {
  22. unsigned int top_num = num * num;
  23. unsigned int current_num = 1;
  24. unsigned int i, j, dir = 0, times = 0;
  25. int curX = 0, curY = 0;
  26.  
  27. int matrix[num][num];
  28. for (i = 0; i < num; i++)
  29. for (j = 0; j < num; j++)
  30. matrix[i][j] = 0;
  31.  
  32. // 0 = right
  33. // 1 = down
  34. // 2 = left
  35. // 3 = up
  36.  
  37. while (current_num <= top_num) {
  38. matrix[curX][curY] = current_num;
  39. current_num++;
  40.  
  41. switch (dir) {
  42. case 0:
  43. curX++;
  44. break;
  45. case 1:
  46. curY++;
  47. break;
  48. case 2:
  49. curX--;
  50. break;
  51. case 3:
  52. curY--;
  53. break;
  54. default:
  55. fprintf(stderr, "error\n");
  56. break;
  57. }
  58.  
  59. if (curX >= num - times && dir == 0) {
  60. curX--;
  61. curY++;
  62. dir = 1;
  63. } else if ((curX < 0 && dir == 2) || (times > 0 && (curX < times && dir == 2))) {
  64. curX++;
  65. curY--;
  66. dir = 3;
  67. } else if (curY >= num - times && dir == 1) {
  68. curY--;
  69. curX--;
  70. dir = 2;
  71. } else if ((curY < 0 && dir == 3) || (times > 0 && (curY < times && dir == 3))) {
  72. curY++;
  73. curX++;
  74. dir = 0;
  75. }
  76.  
  77. if (matrix[curX][curY] != 0) {
  78. if (curX == curY && dir == 3) {
  79. times++;
  80. curX++;
  81. curY++;
  82. dir = 0;
  83. } else {
  84. break;
  85. }
  86. }
  87.  
  88. }
  89. print(num, matrix);
  90. }
  91.  
  92. int main(void) {
  93. unsigned int num;
  94. while (fscanf(stdin, "%d", &num) == 1) {
  95. if (num < 1) {
  96. fprintf(stderr, "please enter a number greater than 0 (you entered %d)\n", num);
  97. continue;
  98. }
  99. spiral(num);
  100. }
  101. return EXIT_SUCCESS;
  102. }
Success #stdin #stdout 0s 10320KB
stdin
2
4
9
12
20
stdout
1 2 
4 3 
 1  2  3  4 
12 13 14  5 
11 16 15  6 
10  9  8  7 
 1  2  3  4  5  6  7  8  9 
32 33 34 35 36 37 38 39 10 
31 56 57 58 59 60 61 40 11 
30 55 72 73 74 75 62 41 12 
29 54 71 80 81 76 63 42 13 
28 53 70 79 78 77 64 43 14 
27 52 69 68 67 66 65 44 15 
26 51 50 49 48 47 46 45 16 
25 24 23 22 21 20 19 18 17 
  1   2   3   4   5   6   7   8   9  10  11  12 
 44  45  46  47  48  49  50  51  52  53  54  13 
 43  80  81  82  83  84  85  86  87  88  55  14 
 42  79 108 109 110 111 112 113 114  89  56  15 
 41  78 107 128 129 130 131 132 115  90  57  16 
 40  77 106 127 140 141 142 133 116  91  58  17 
 39  76 105 126 139 144 143 134 117  92  59  18 
 38  75 104 125 138 137 136 135 118  93  60  19 
 37  74 103 124 123 122 121 120 119  94  61  20 
 36  73 102 101 100  99  98  97  96  95  62  21 
 35  72  71  70  69  68  67  66  65  64  63  22 
 34  33  32  31  30  29  28  27  26  25  24  23 
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
 76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  21 
 75 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160  95  22 
 74 143 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 161  96  23 
 73 142 203 256 257 258 259 260 261 262 263 264 265 266 267 268 219 162  97  24 
 72 141 202 255 300 301 302 303 304 305 306 307 308 309 310 269 220 163  98  25 
 71 140 201 254 299 336 337 338 339 340 341 342 343 344 311 270 221 164  99  26 
 70 139 200 253 298 335 364 365 366 367 368 369 370 345 312 271 222 165 100  27 
 69 138 199 252 297 334 363 384 385 386 387 388 371 346 313 272 223 166 101  28 
 68 137 198 251 296 333 362 383 396 397 398 389 372 347 314 273 224 167 102  29 
 67 136 197 250 295 332 361 382 395 400 399 390 373 348 315 274 225 168 103  30 
 66 135 196 249 294 331 360 381 394 393 392 391 374 349 316 275 226 169 104  31 
 65 134 195 248 293 330 359 380 379 378 377 376 375 350 317 276 227 170 105  32 
 64 133 194 247 292 329 358 357 356 355 354 353 352 351 318 277 228 171 106  33 
 63 132 193 246 291 328 327 326 325 324 323 322 321 320 319 278 229 172 107  34 
 62 131 192 245 290 289 288 287 286 285 284 283 282 281 280 279 230 173 108  35 
 61 130 191 244 243 242 241 240 239 238 237 236 235 234 233 232 231 174 109  36 
 60 129 190 189 188 187 186 185 184 183 182 181 180 179 178 177 176 175 110  37 
 59 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111  38 
 58  57  56  55  54  53  52  51  50  49  48  47  46  45  44  43  42  41  40  39