fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const int N = 13, N2 = N*N+1, NS = N*N/2+1, N_ = N - 1;
  9. int M[N][N];
  10. int x = 0, y = 0, s = 0;
  11. for(int i = 1; i <= NS; ++i)
  12. {
  13. M[y][x] = i;
  14. M[N_-y][N_-x] = N2-i;
  15. if (s) { ++x; y -= (s = y != 0); }
  16. else { ++y; x -= !(s = x == 0); }
  17. }
  18.  
  19. for(int r = 0; r < N; ++r)
  20. {
  21. for(int c = 0; c < N; ++c)
  22. {
  23. cout << setw(4) << M[r][c];
  24. }
  25. cout << endl;
  26. }
  27. }
  28.  
Success #stdin #stdout 0.01s 5512KB
stdin
Standard input is empty
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