forked from xinw3/leetcode-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSquare.java
More file actions
35 lines (29 loc) · 933 Bytes
/
MagicSquare.java
File metadata and controls
35 lines (29 loc) · 933 Bytes
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
public class MagicSquare {
public static void main(String[] args) {
int n = 3;
int[][] square = new int[n][n];
getSquare(square, n-1, n/2);
printSquare(square);
}
public static void getSquare(int[][] square, int row, int col) {
int n = square.length;
square[row][col] = 1;
for (int i = 2; i <= n*n; i++) {
if (square[(row+1) % n][(col+1) % n] == 0) {
row = (row+1) % n;
col = (col+1) % n;
} else {
row = (row+n-1) % n;
}
square[row][col] = i;
}
}
public static void printSquare(int[][] square) {
for (int i = 0; i < square.length; i++) {
for (int j = 0; j < square[0].length; j++) {
System.out.print(square[i][j] + " ");
}
System.out.println();
}
}
}