fork download
  1. #include <stdio.h>
  2.  
  3. int f(int n,int a[][n],int *b){static int d,j,k,s,f,i;int m=n-1;for(;i<n*n;i++,s++){if(s==m){d++;d%=4;s=0;f++;if(f%3==0){m--;f=1;}}b[i]=!d?a[j][k++]:d==1?a[j++][k]:d==2?a[j][k--]:a[j--][k];}}
  4.  
  5. /* The function from RosettaCode. */
  6. int spiral(int w, int h, int x, int y) {
  7. return y ? w + spiral(h - 1, w, y - 1, w - x - 1) : x;
  8. }
  9.  
  10. int main(void) {
  11. #define N 7
  12.  
  13. /* Generation of the input array. */
  14. #if N == 0
  15. int a[1][1];
  16. #else
  17. int a[N][N];
  18.  
  19. for (size_t i = 0; i < N; i++) {
  20. for (size_t j = 0; j < N; j++) {
  21. a[i][j] = spiral(N, N, j, i) + 1;
  22. printf("%4d", a[i][j]);
  23. }
  24.  
  25. putchar('\n');
  26. }
  27. #endif
  28.  
  29. /* Declaration of the output array. */
  30. #if N != 0
  31. int b[N*N];
  32. #else
  33. int b[1];
  34. #endif
  35.  
  36. /* Process. */
  37. f(N, a, b);
  38.  
  39. /* Print the result. */
  40. #if N != 0
  41. putchar('\n');
  42. for (size_t i = 0; i < N*N; i++)
  43. printf("%d ", b[i]);
  44. #endif
  45. }
  46.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
   1   2   3   4   5   6   7
  24  25  26  27  28  29   8
  23  40  41  42  43  30   9
  22  39  48  49  44  31  10
  21  38  47  46  45  32  11
  20  37  36  35  34  33  12
  19  18  17  16  15  14  13

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49