Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. #define MAX 100
  5.  
  6. void maPhuongBacLe(int A[][MAX+1], int n)
  7. {
  8. int i = 1;
  9. int j = n / 2 + 1;
  10. int k = 1;
  11. int n2 = n*n;
  12. while (k <= n2)
  13. {
  14. A[i][j] = k;
  15.  
  16. if (k % n == 0)
  17. {
  18. i = i + 1;
  19. if ( i > n ) i = i - n;
  20. }
  21. else
  22. {
  23. if (i == 1)
  24. i = n;
  25. else
  26. i--;
  27. if (j == n)
  28. j = 1;
  29. else
  30. j++;
  31. }
  32. k++;
  33. }
  34. }
  35.  
  36. void maPhuongBacChanChan(int A[][MAX+1], int n)
  37. {
  38. int k = 1;
  39. int i = 1;
  40. int j = 1;
  41. int n2 = n * n;
  42.  
  43. do
  44. {
  45. if ( (i - j) % 4 == 0 || (i + j -1) % 4 == 0)
  46. A[i][j] = k;
  47. else
  48. A[i][j] = n2 + 1 - k;
  49.  
  50. k++;
  51.  
  52. if (j==n)
  53. {
  54. j = 1;
  55. i++;
  56. }
  57. else
  58. j++;
  59. }
  60. while (k <= n2);
  61. }
  62.  
  63. void LUX(int A[][MAX+1], int i, int j, int num, char ch)
  64. {
  65. num = 1 + (num-1)*4;
  66. i = 1 + (i-1)*2;
  67. j = 1 + (j-1)*2;
  68. switch (ch)
  69. {
  70. case 'L':
  71. A[i][j] = num + 3;
  72. A[i][j+1] = num;
  73. A[i+1][j] = num + 1;
  74. A[i+1][j+1] = num + 2;
  75. break;
  76. case 'U':
  77. A[i][j] = num;
  78. A[i][j+1] = num + 3;
  79. A[i+1][j] = num + 1;
  80. A[i+1][j+1] = num + 2;
  81. break;
  82. case 'X':
  83. A[i][j] = num;
  84. A[i][j+1] = num + 3;
  85. A[i+1][j] = num + 2;
  86. A[i+1][j+1] = num + 1;
  87. }
  88. }
  89.  
  90. void maPhuongBacChanLe(int A[][MAX+1], int n)
  91. {
  92. int m = n / 2;
  93. int B[MAX+1][MAX+1];
  94. maPhuongBacLe(B, m);
  95.  
  96. char ch;
  97. int m2 = m / 2;
  98. int i, j;
  99. for (i = 1; i <= m; i++)
  100. for (j = 1; j <= m; j++)
  101. {
  102. if (i <= m2)
  103. ch = 'L';
  104. else if (i == m2 + 1)
  105. {
  106. if (j == m2+1)
  107. ch = 'U';
  108. else
  109. ch = 'L';
  110. }
  111. else if (i == m2 + 2)
  112. {
  113. if (j == m2 + 1)
  114. ch = 'L';
  115. else
  116. ch = 'U';
  117. }
  118. else
  119. ch = 'X';
  120. LUX(A, i, j, B[i][j], ch);
  121. }
  122.  
  123. }
  124.  
  125. int main()
  126. {
  127.  
  128. int A[MAX+1][MAX+1];
  129. int n;
  130. do
  131. {
  132. cout << "n = ";
  133. cin >> n;
  134. }
  135. while (n < 3 || n > MAX);
  136.  
  137. if (n % 2 == 1)
  138. {
  139. maPhuongBacLe(A, n);
  140. }
  141. else if (n % 4 == 0)
  142. {
  143. maPhuongBacChanChan(A, n);
  144. }
  145. else
  146. {
  147. maPhuongBacChanLe(A, n);
  148. }
  149.  
  150. int i, j;
  151.  
  152. for (i = 1; i <= n; i++)
  153. {
  154. for (j = 1; j <= n; j++)
  155. cout << setw(3) << A[i][j] << " ";
  156. cout << endl;
  157. }
  158.  
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement