-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagicSquare.java
84 lines (70 loc) · 1.62 KB
/
MagicSquare.java
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
package main;
/**
*
* @author Ivan Petrov
*
*/
public class MagicSquare {
private static int getSum(int[][] matrix) {
int sum = 0;
for (int i = 0; i < 1; i++) {
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
}
return sum;
}
private static boolean checkRows(int[][] matrix, int sum) {
int rowSum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
rowSum += matrix[i][j];
}
if (rowSum != sum) {
return false;
} else {
rowSum = 0;
}
}
return true;
}
private static boolean checkCols(int[][] matrix, int sum) {
int colSum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
colSum += matrix[j][i];
}
if (colSum != sum) {
return false;
} else {
colSum = 0;
}
}
return true;
}
private static boolean checkDiagonals(int[][] matrix, int sum) {
int mainSum = 0, secondSum = 0;
for (int i = 0; i < matrix.length; i++) {
mainSum += matrix[i][i];
secondSum += matrix[i][matrix.length - 1 - i];
}
if (mainSum != sum || secondSum != sum) {
return false;
}
return true;
}
public static boolean magic_square(int[][] matrix) {
int sum = getSum(matrix);
if (checkRows(matrix, sum) && checkCols(matrix, sum) && checkDiagonals(matrix, sum)) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
int[][] matrix = {{4,9,2},{3,5,7},{8,1,6}};
int[][] matrix1 = {{4,92,2},{3,5,7},{8,1,6}};
System.out.println(magic_square(matrix));
System.out.println(magic_square(matrix1));
}
}