-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMagicSquareOdd.java
More file actions
41 lines (39 loc) · 851 Bytes
/
MagicSquareOdd.java
File metadata and controls
41 lines (39 loc) · 851 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
36
37
38
39
40
41
import java.io.*;
import java.util.*;
public class MagicSquareOdd {
public static void main(String[] args) throws Exception {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
Scanner s = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = 4;
int[][] ms = new int[n][n];
int val = 1;
int x = 0, y = n/2-1;
while(val <= n*n) {
if(ms[x][y] != 0) {
if(x != n-1) {
x++;
} else {
x = 0;
}
ms[x][y] = val++;
} else {
ms[x][y] = val++;
}
if(x-1 < 0) {
x = n-1;
} else {
x--;
}
if(y == n-1) {
y = 0;
} else {
y++;
}
}
for(int i = 0; i < n; i++)
for(int j =0; j < n; j++)
if(j != n-1) System.out.print(ms[i][j] + " ");
else System.out.println(ms[i][j]);
}
}