-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseDiagonal.java
52 lines (42 loc) · 1.47 KB
/
ReverseDiagonal.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
package Esercizi_Esami;
public class ReverseDiagonal {
public static void main(String[] args) {
int[][] matrice = {{1,2,3,7},
{4,5,6,8},
{7,2,9,3},
{5,8,6,4}};
int[][] matrix = {{1,2,3,4,5},
{6,7,8,9,0},
{1,2,3,4,5},
{6,7,8,9,0},
{1,2,3,4,5}};
//reverseDiagonal(matrice);
//visualizza(matrice);
//reverseDiagonal(matrix);
//visualizza(matrix);
sposta(matrix);
visualizza(matrix);
}
public static void visualizza(int[][] m) {
for (int i = 0; i < m.length; i++) {
for(int j = 0; j < m[i].length; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
public static void reverseDiagonal(int[][] m){
for (int i = 0; i < m.length / 2; i++) {
int temp = m[i][i];
m[i][i] = m[m.length-1-i][m.length-1-i];
m[m.length-1-i][m.length-1-i] = temp;
}
}
public static void sposta(int[][] m){
for (int i = 0; i < m.length - 1; i++) {
int temp = m[i][i];
m[i][i] = m[(m.length - 1) / 2 - 1 + i][(m.length - 1) / 2 - 1 + i];
m[(m.length - 1) / 2 - 1 + i][(m.length - 1) / 2 - 1 + i] = temp;
}
}
}