fork download
  1. #include <stdio.h>
  2. main(){int i,N,x=0,y=0,s=0;scanf("%d",&N);int M[N][N];
  3. for(i=1;i<=N*N/2+1;){M[y][x]=i;M[N-1-y][N-1-x]=N*N+1-i++;
  4. if(s)++x,y-=s=!!y;else ++y,x-=!(s=!x);}
  5. for(i=0;i<N;++i){for(s=0;s<N;)printf("%d ",M[i][s++]);puts("");}}
  6.  
Success #stdin #stdout 0s 5532KB
stdin
13
stdout
1 3 4 10 11 21 22 36 37 55 56 78 79 
2 5 9 12 20 23 35 38 54 57 77 80 103 
6 8 13 19 24 34 39 53 58 76 81 102 104 
7 14 18 25 33 40 52 59 75 82 101 105 124 
15 17 26 32 41 51 60 74 83 100 106 123 125 
16 27 31 42 50 61 73 84 99 107 122 126 141 
28 30 43 49 62 72 85 98 108 121 127 140 142 
29 44 48 63 71 86 97 109 120 128 139 143 154 
45 47 64 70 87 96 110 119 129 138 144 153 155 
46 65 69 88 95 111 118 130 137 145 152 156 163 
66 68 89 94 112 117 131 136 146 151 157 162 164 
67 90 93 113 116 132 135 147 150 158 161 165 168 
91 92 114 115 133 134 148 149 159 160 166 167 169