import java util Scanner public class Task38 public static void makeRo

 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
import java.util.Scanner;
public class Task38 {
public static void makeRow(int rowNum, int[][] a){
int n = a.length;
if (rowNum == n)
return;
if (a[rowNum][rowNum] == 0) {
int[] c = new int[n];
boolean pr = false;
for (int d = rowNum + 1; d < n && !pr; d++) {
if (a[d][rowNum] != 0) {
c = a[d];
a[d] = a[rowNum];
a[rowNum] = c;
pr = !pr;
}
}
}
int k = 1;
if (a[rowNum][rowNum] != 0) {
for (int x = rowNum + 1; x < n; x++) {
k = a[x][rowNum];
for (int j = rowNum; j < n; j++) {
a[x][j] = a[x][j] * a[rowNum][rowNum] - a[rowNum][j] * k;
}
}
}
makeRow(rowNum + 1, a);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = scanner.nextInt();
}
}
for (int i = 0; i < n; i++) {
if (a[i][i] == 0) {
int[] c = new int[n];
boolean pr = false;
for (int d = i + 1; d < n && !pr; d++) {
if (a[d][i] != 0) {
c = a[d];
a[d] = a[i];
a[i] = c;
pr = !pr;
}
}
}
int k = 1;
if (a[i][i] != 0) {
for (int x = i + 1; x < n; x++) {
k = a[x][i];
for (int j = i; j < n; j++) {
a[x][j] = a[x][j] * a[i][i] - a[i][j] * k;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}