-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic_square_module.cpp
More file actions
140 lines (113 loc) · 4.07 KB
/
Copy pathmagic_square_module.cpp
File metadata and controls
140 lines (113 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
void OutputSquareConsole(int n, const std::vector<std::vector<int>>& magic_square);
void OutputSquareFile(int n, const std::vector<std::vector<int>>& magic_square);
int CountDigits(int number);
bool IsMagicSquareValid(const std::vector<std::vector<int>>& square);
void GenerateMagicSquare(int square_size) {
const int n = square_size;
std::vector<std::vector<int>> magic_square(n, std::vector<int>(n, 0));
int i = 0;
int j = n / 2;
for (int num = 1; num <= n * n; ++num) {
magic_square[i][j] = num;
const int next_i = (i - 1 + n) % n;
const int next_j = (j + 1) % n;
if (magic_square[next_i][next_j] != 0) {
i = (i + 1) % n;
} else {
i = next_i;
j = next_j;
}
}
if (IsMagicSquareValid(magic_square)) {
std::cout << "✓ Квадрат корректный!\n";
} else {
std::cout << "✗ Ошибка: квадрат некорректный!\n";
}
OutputSquareConsole(n, magic_square);
OutputSquareFile(n, magic_square);
}
void OutputSquareConsole(int n, const std::vector<std::vector<int>>& magic_square) {
std::cout << "Магический квадрат порядка " << n << ":\n";
std::cout << "Сумма в каждой строке, столбце и диагонали = "
<< n * (n * n + 1) / 2 << "\n\n";
const int column_width = CountDigits(n * n) + 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
std::cout << std::setw(column_width) << magic_square[i][j] << " ";
}
std::cout << "\n";
}
}
void OutputSquareFile(int n, const std::vector<std::vector<int>>& magic_square) {
std::ofstream output_file("magic_square.txt");
if (!output_file.is_open()) {
std::cerr << "Ошибка: не удалось открыть файл для записи.\n";
return;
}
output_file << "Магический квадрат порядка " << n << ":\n";
output_file << "Сумма в каждой строке, столбце и диагонали = "
<< n * (n * n + 1) / 2 << "\n\n";
const int column_width = CountDigits(n * n) + 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
output_file << std::setw(column_width) << magic_square[i][j] << " ";
}
output_file << "\n";
}
}
int CountDigits(int number) {
if (number == 0) return 1;
if (number < 0) number = -number;
return static_cast<int>(std::log10(number)) + 1;
}
void DisplaySquareInfo() {
std::cout << "\n\n Магический квадрат — это квадратная таблица размером n×n,\n"
<< " заполненная разными натуральными числами (обычно от 1 до n²)\n"
<< " таким образом, что сумма чисел в каждой строке, каждом столбце\n"
<< " и на обеих диагоналях — одинаковая.\n\n";
}
bool IsMagicSquareValid(const std::vector<std::vector<int>>& square) {
const int n = square.size();
if (n <= 0 || n % 2 == 0) return false;
std::vector<bool> found(n * n + 1, false);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
const int num = square[i][j];
if (num < 1 || num > n * n || found[num]) {
return false;
}
found[num] = true;
}
}
const int magic_sum = n * (n * n + 1) / 2;
for (int i = 0; i < n; ++i) {
int row_sum = 0;
for (int j = 0; j < n; ++j) {
row_sum += square[i][j];
}
if (row_sum != magic_sum) return false;
}
for (int j = 0; j < n; ++j) {
int col_sum = 0;
for (int i = 0; i < n; ++i) {
col_sum += square[i][j];
}
if (col_sum != magic_sum) return false;
}
int diag1_sum = 0;
for (int i = 0; i < n; ++i) {
diag1_sum += square[i][i];
}
if (diag1_sum != magic_sum) return false;
int diag2_sum = 0;
for (int i = 0; i < n; ++i) {
diag2_sum += square[i][n - 1 - i];
}
if (diag2_sum != magic_sum) return false;
return true;
}