fork download
  1. #include <stdio.h>
  2.  
  3. int f(int n,int a[][n],int*b){int d=0,j=0,k=0,s=0,m=n-1,f=0;for(int i=0;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. int main(void) {
  6. #define N 5
  7.  
  8. /* The input array. */
  9. #if N == 0
  10. int a[1][1];
  11. #elif N == 1
  12. int a[N][N] = {{1}};
  13. #elif N == 2
  14. int a[N][N] = {{1, 2}, {4, 3}};
  15. #elif N == 3
  16. int a[N][N] = {{1, 2, 3}, {8, 9, 4}, {7, 6, 5}};
  17. #elif N == 4
  18. int a[N][N] = {{ 1, 2, 3, 4}, {12, 13, 14, 5}, {11, 16, 15, 6}, {10, 9, 8, 7}};
  19. #elif N == 5
  20. int a[N][N] = {{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}};
  21. #endif
  22.  
  23. /* The output array. */
  24. #if N != 0
  25. int b[N*N];
  26. #else
  27. int b[1];
  28. #endif
  29.  
  30. /* Process. */
  31. f(N, a, b);
  32.  
  33. /* Print the result. */
  34. #if N != 0
  35. for (size_t i = 0; i < N*N; i++)
  36. printf("%d ", b[i]);
  37. #endif
  38. }
  39.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
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