PloadyFree

Verificator

Sep 22nd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     private Scanner in = new Scanner(System.in);
  5.  
  6.     public static void main(String[] args) {
  7.         new Main().solve();
  8.     }
  9.  
  10.     private void solve() {
  11.         int[][] a = input(4);
  12.         System.out.println(squaresWithRow(a, in.nextInt()));
  13.         System.out.println(squaresWithColumn(a, in.nextInt()));
  14.     }
  15.  
  16.     /**
  17.      * Находит определитель третьего порядка
  18.      *
  19.      * @param a Матрица 3х3
  20.      * @return Определитель 3-го порядка
  21.      */
  22.     private int triangles(int[][] a) {
  23.         int plus = 0;
  24.         for (int i = 0; i < 3; i++) {
  25.             int diag = 1;
  26.             for (int j = 0; j < 3; j++)
  27.                 diag *= a[j][(i + j) % 3];
  28.             plus += diag;
  29.         }
  30.  
  31.         int minus = 0;
  32.         for (int i = 2; i >= 0; i--) {
  33.             int diag = 1;
  34.             for (int j = 0; j < 3; j++)
  35.                 diag *= a[j][(i - j + 3) % 3];
  36.             minus += diag;
  37.         }
  38.  
  39.         return plus - minus;
  40.     }
  41.  
  42.     /**
  43.      * Находит определитель 4-го порядка через строку
  44.      *
  45.      * @param a Матрица 4х4
  46.      * @param mainRow Основная строка
  47.      * @return Определитель 4-го порядка через строку
  48.      */
  49.     private int squaresWithRow(int[][] a, int mainRow) {
  50.         int[][] matr = new int[3][3];
  51.         int ans = 0;
  52.         for (int columnExclude = 0; columnExclude < 4; columnExclude++) {
  53.  
  54.             int colIdx;
  55.             int rowIdx = 0;
  56.  
  57.             for (int i = 0; i < 4; i++) {
  58.                 colIdx = 0;
  59.                 if (i == mainRow) continue;
  60.                 for (int j = 0; j < 4; j++) {
  61.                     if (j == columnExclude) continue;
  62.                     matr[rowIdx][colIdx++] = a[i][j];
  63.                 }
  64.                 rowIdx++;
  65.             }
  66.  
  67.             int tr = triangles(matr);
  68.             tr *= a[mainRow][columnExclude];
  69.             if ((mainRow + columnExclude) % 2 != 0) tr *= -1;
  70.             ans += tr;
  71.         }
  72.  
  73.         return ans;
  74.     }
  75.  
  76.     /**
  77.      * Находит определитель 4-го порядка через столбец
  78.      *
  79.      * @param a Матрица 4х4
  80.      * @param mainCol Основная колонка
  81.      * @return Определитель 4-го порядка через столбец
  82.      */
  83.     private int squaresWithColumn(int[][] a, int mainCol) {
  84.         int[][] matr = new int[3][3];
  85.         int ans = 0;
  86.         for (int rowExclude = 0; rowExclude < 4; rowExclude++) {
  87.  
  88.             int rowIdx = 0;
  89.             int colIdx;
  90.  
  91.             for (int i = 0; i < 4; i++) {
  92.                 colIdx = 0;
  93.                 if (i == rowExclude) continue;
  94.                 for (int j = 0; j < 4; j++) {
  95.                     if (j == mainCol) continue;
  96.                     matr[rowIdx][colIdx++] = a[i][j];
  97.                 }
  98.                 rowIdx++;
  99.             }
  100.  
  101.             int tr = triangles(matr);
  102.             tr *= a[rowExclude][mainCol];
  103.             if ((mainCol + rowExclude) % 2 != 0) tr *= -1;
  104.             ans += tr;
  105.         }
  106.  
  107.         return ans;
  108.     }
  109.  
  110.     /**
  111.      * Вводит таблицу с консоли
  112.      *
  113.      * @param size Размер матрицы для ввода
  114.      * @return Матрицу
  115.      */
  116.     private int[][] input(int size) {
  117.         int[][] a = new int[size][size];
  118.         for (int i = 0; i < size; i++)
  119.             for (int j = 0; j < size; j++)
  120.                 a[i][j] = in.nextInt();
  121.  
  122.         return a;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment