Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- private Scanner in = new Scanner(System.in);
- public static void main(String[] args) {
- new Main().solve();
- }
- private void solve() {
- int[][] a = input(4);
- System.out.println(squaresWithRow(a, in.nextInt()));
- System.out.println(squaresWithColumn(a, in.nextInt()));
- }
- /**
- * Находит определитель третьего порядка
- *
- * @param a Матрица 3х3
- * @return Определитель 3-го порядка
- */
- private int triangles(int[][] a) {
- int plus = 0;
- for (int i = 0; i < 3; i++) {
- int diag = 1;
- for (int j = 0; j < 3; j++)
- diag *= a[j][(i + j) % 3];
- plus += diag;
- }
- int minus = 0;
- for (int i = 2; i >= 0; i--) {
- int diag = 1;
- for (int j = 0; j < 3; j++)
- diag *= a[j][(i - j + 3) % 3];
- minus += diag;
- }
- return plus - minus;
- }
- /**
- * Находит определитель 4-го порядка через строку
- *
- * @param a Матрица 4х4
- * @param mainRow Основная строка
- * @return Определитель 4-го порядка через строку
- */
- private int squaresWithRow(int[][] a, int mainRow) {
- int[][] matr = new int[3][3];
- int ans = 0;
- for (int columnExclude = 0; columnExclude < 4; columnExclude++) {
- int colIdx;
- int rowIdx = 0;
- for (int i = 0; i < 4; i++) {
- colIdx = 0;
- if (i == mainRow) continue;
- for (int j = 0; j < 4; j++) {
- if (j == columnExclude) continue;
- matr[rowIdx][colIdx++] = a[i][j];
- }
- rowIdx++;
- }
- int tr = triangles(matr);
- tr *= a[mainRow][columnExclude];
- if ((mainRow + columnExclude) % 2 != 0) tr *= -1;
- ans += tr;
- }
- return ans;
- }
- /**
- * Находит определитель 4-го порядка через столбец
- *
- * @param a Матрица 4х4
- * @param mainCol Основная колонка
- * @return Определитель 4-го порядка через столбец
- */
- private int squaresWithColumn(int[][] a, int mainCol) {
- int[][] matr = new int[3][3];
- int ans = 0;
- for (int rowExclude = 0; rowExclude < 4; rowExclude++) {
- int rowIdx = 0;
- int colIdx;
- for (int i = 0; i < 4; i++) {
- colIdx = 0;
- if (i == rowExclude) continue;
- for (int j = 0; j < 4; j++) {
- if (j == mainCol) continue;
- matr[rowIdx][colIdx++] = a[i][j];
- }
- rowIdx++;
- }
- int tr = triangles(matr);
- tr *= a[rowExclude][mainCol];
- if ((mainCol + rowExclude) % 2 != 0) tr *= -1;
- ans += tr;
- }
- return ans;
- }
- /**
- * Вводит таблицу с консоли
- *
- * @param size Размер матрицы для ввода
- * @return Матрицу
- */
- private int[][] input(int size) {
- int[][] a = new int[size][size];
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- a[i][j] = in.nextInt();
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment