fork(1) download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int n = 4, m = 4;
  5. int arr[4][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  6.  
  7. int top = 0, bottom = n-1, right = m-1, left = 0, i=0, j=0;
  8.  
  9. printf("%d ", arr[i][j]);
  10.  
  11. while(1){
  12. if(top>bottom || left>right) break;
  13.  
  14.  
  15. if(bottom==top){
  16. for(j=left; j<=right; j++)
  17. printf("%d ", arr[top][j]);
  18. break;
  19. }
  20.  
  21. if(right==left){
  22. for(i=top; i<=bottom; i++)
  23. printf("%d ", arr[left][i]);
  24. break;
  25. }
  26.  
  27. int first = 1;
  28. while(j<=right){
  29. if(first){
  30. j++;
  31. first = 0;
  32. continue;
  33. }
  34.  
  35. printf("%d ", arr[i][j]);
  36. j++;
  37. }
  38. j--; right--;
  39.  
  40. first = 1;
  41. while(i<=bottom){
  42. if(first){
  43. i++;
  44. first = 0;
  45. continue;
  46. }
  47.  
  48. printf("%d ", arr[i][j]);
  49. i++;
  50. }
  51. i--; bottom--;
  52.  
  53. first = 1;
  54. while(j>=left){
  55. if(first){
  56. j--;
  57. first = 0;
  58. continue;
  59. }
  60.  
  61. printf("%d ", arr[i][j]);
  62. j--;
  63. }
  64. j++; left++;
  65.  
  66. first = 1;
  67. while(i>=top+1){
  68. if(first){
  69. i--;
  70. first = 0;
  71. continue;
  72. }
  73.  
  74. printf("%d ", arr[i][j]);
  75. i--;
  76. }
  77. i++; top++;
  78. }
  79.  
  80. return 0;
  81. }
  82.  
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10