-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2.c
More file actions
87 lines (71 loc) · 1.74 KB
/
Array2.c
File metadata and controls
87 lines (71 loc) · 1.74 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
#include <stdio.h>
int main() {
int n;
printf("홀수 n을 입력하세요: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("홀수만 입력하세요.\n");
return 1;
}
int arr[n][n];
// 배열 0으로 초기화
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = 0;
}
}
int num = 1;
int i = 0;
int j = n / 2;
// 마방진 채우기
while (num <= n * n) {
arr[i][j] = num;
// 다음 위치 칸 계산
int next_i = (i - 1 + n) % n;
int next_j = (j + 1) % n;
// 이미 값이 있다면 아래로 이동
if (arr[next_i][next_j] != 0) {
i = (i + 1) % n;
} else {
i = next_i;
j = next_j;
}
num++;
}
// 결과
printf("\n[마방진]\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
// 가로 합
printf("\n[가로 합]\n");
for (i = 0; i < n; i++) {
int sum = 0;
for (j = 0; j < n; j++) {
sum += arr[i][j];
}
printf("%d 행: %d\n", i + 1, sum);
}
// 세로 합
printf("\n[세로 합]\n");
for (j = 0; j < n; j++) {
int sum = 0;
for (i = 0; i < n; i++) {
sum += arr[i][j];
}
printf("%d 열: %d\n", j + 1, sum);
}
// 대각선 합
int d1 = 0, d2 = 0;
for (i = 0; i < n; i++) {
d1 += arr[i][i];
d2 += arr[i][n - 1 - i];
}
printf("\n[대각선 합]\n");
printf("왼 위 -> 오 아래 대각선: %d\n", d1);
printf("오 위 -> 왼 아래 대각선: %d\n", d2);
return 0;
}