fork download
  1. #include <stdio.h>
  2.  
  3. int f(int n,int a[][n],int*b){static d,j,k,s,f,i,m;for(m=n-1;i<n*n;i++,s++)s==m?d++,d%=4,s=0,f++,f%3==0?m--,f=1:m:m,b[i]=!d?a[j][k++]:d==1?a[j++][k]:d==2?a[j][k--]:a[j--][k];}
  4.  
  5. /* The spiral 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 5
  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
  16  17  18  19   6
  15  24  25  20   7
  14  23  22  21   8
  13  12  11  10   9

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