-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic_square.cpp
More file actions
69 lines (62 loc) · 1.33 KB
/
Copy pathmagic_square.cpp
File metadata and controls
69 lines (62 loc) · 1.33 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
#include <iomanip>
#include<iostream>
#include<vector>
using namespace std;
int main() {
int num;
cout << "홀수 숫자를 하나 입력해주세요 : ";
cin >> num;
vector<vector<int>> magic_square(num + 1, vector<int>(num + 1));
magic_square[0][(num / 2) + 1] = 1;
magic_square[num][(num / 2) + 1] = 1;
int row = num - 1;
int col = (num / 2) + 2;
int current_num = 1;
while (current_num < (num) * (num)) {
if (magic_square[row][col] == 0) {
magic_square[row][col] = current_num + 1;
row--;
col++;
if (row == -1 && col == num + 1) {
row++;
col--;
}
else if (row == -1) {
row++;
col--;
}
else if (col == num + 1) {
row++;
col--;
}
else if (magic_square[row][col] != 0) {
row++;
col--;
}
current_num++;
}
else if (row == 0 && col == num) {
row++;
}
else if (col == num) {
magic_square[row][0] = magic_square[row][col];
col = 1;
row--;
}
else if (row == 0) {
magic_square[row + num][col] = magic_square[row][col];
row = num - 1;
col++;
}
else if (magic_square[row][col] <= num * num && magic_square[row][col] >= 1) {
row++;
}
}
for (int i = 0; i < num; i++) {
for (int j = 1; j <= num; j++) {
cout << setw(3) << magic_square[i][j];
}
cout << endl;
}
return 0;
}