fork(3) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. const int MAXN = 49;
  8.  
  9. int a [MAXN + 2][MAXN + 2] = {0};
  10.  
  11. int main ()
  12. {
  13. int n;
  14. cin >> n;
  15.  
  16. if ((n - (n * n / 2) / (n - 1)) % 2 != 0)
  17. {
  18. int odd = 1, even = 2, change = (n / 2) * n + 1;
  19. for (int i = 1; i <= n; i++)
  20. {
  21. if (i == n / 2 + 1)
  22. {
  23. for (int j = 1; j <= n; j++)
  24. {
  25. a [i][j] = odd;
  26. odd += 2;
  27. }
  28. }
  29. else
  30. {
  31. for (int j = 1; j <= n; j++)
  32. {
  33. if (j % 2 != 0)
  34. {
  35. a [i][j] = even;
  36. even += 2;
  37. }
  38. else
  39. {
  40. a [i][j] = odd;
  41. odd += 2;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. else
  48. {
  49. int even = 2, odd = 1, start = n / 2 + 2, end = n / 2;
  50. for (int i = 1; i <= n; i++)
  51. {
  52. a [i][i] = odd;
  53. odd += 2;
  54. }
  55.  
  56. for (int i = 1; i <= end; i++)
  57. {
  58. for (int j = start; j <= n; j++)
  59. {
  60. a [i][j] = odd;
  61. odd += 2;
  62. }
  63. }
  64. for (int i = start; i <= n; i++)
  65. {
  66. for (int j = 1; j <= end; j++)
  67. {
  68. a [i][j] = odd;
  69. odd += 2;
  70. }
  71. }
  72. for (int i = 1; i <= n; i++)
  73. {
  74. for (int j = 1; j <= n; j++)
  75. {
  76. if (a [i][j] == 0)
  77. {
  78. a [i][j] = even;
  79. even += 2;
  80. }
  81. }
  82. }
  83. }
  84.  
  85. int width = (int) log ((double) n * n) / log (10.0) + 1;
  86. //cout << width << "\n";
  87. for (int i = 1; i <= n; i++)
  88. {
  89. for (int j = 1; j <= n; j++)
  90. cout << setw (width) << a [i][j] << " ";
  91. cout << "\n";
  92. }
  93.  
  94. return 0;
  95. }
Runtime error #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Standard output is empty