Advertisement
Filip_Markoski

[NP] 2.1 Матрица од реални броеви (Solved)

Oct 24th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.08 KB | None | 0 0
  1. import java.io.ByteArrayInputStream;
  2. import java.io.InputStream;
  3. import java.text.DecimalFormat;
  4. import java.util.Arrays;
  5. import java.util.Comparator;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8. import java.util.stream.IntStream;
  9.  
  10. /* Exceptions */
  11. class InsufficientElementsException extends Exception {
  12.     /*
  13.     public InsufficientElementsException () {};
  14.     public String getMessage() { return String.format("Insufficient number of elements"); }
  15.      */
  16.     @Override
  17.     public String getMessage() {
  18.         return "Insufficient number of elements";
  19.     }
  20. }
  21.  
  22. class InvalidRowNumberException extends Exception {
  23.     public InvalidRowNumberException() {
  24.     }
  25.  
  26.     ;
  27.  
  28.     public String getMessage() {
  29.         return String.format("Invalid row number");
  30.     }
  31. }
  32.  
  33. class InvalidColumnNumberException extends Exception {
  34.     public InvalidColumnNumberException() {
  35.     }
  36.  
  37.     ;
  38.  
  39.     public String getMessage() {
  40.         return String.format("Invalid column number");
  41.     }
  42. }
  43.  
  44. final class DoubleMatrix {
  45.     private double matrix[][];
  46.  
  47.     public DoubleMatrix(double a[], int width, int height) throws InsufficientElementsException {
  48.         if (a.length < width * height) {
  49.             throw new InsufficientElementsException();
  50.         }
  51.         matrix = new double[width][height];
  52.         int k = a.length - 1;
  53.         for (int i = width - 1; i >= 0; i--) {
  54.             for (int j = height - 1; j >= 0; j--) {
  55.                 matrix[i][j] = a[k--];
  56.             }
  57.         }
  58.     }
  59.  
  60.     public String getDimensions() {
  61.         return String.format("[%d x %d]", matrix.length, matrix[0].length);
  62.     }
  63.  
  64.     public int rows() {
  65.         return matrix.length;
  66.     }
  67.  
  68.     public int columns() {
  69.         return matrix[0].length;
  70.     }
  71.  
  72.     public double maxElementAtRow(int row) throws InvalidRowNumberException {
  73.         /* Assuming the row is given starting from 1 and not 0 */
  74.         row -= 1;
  75.         if (row < 0 || row >= matrix.length) {
  76.             throw new InvalidRowNumberException();
  77.         }
  78.         /*
  79.         double max = matrix[row][0];
  80.         int len = matrix[row].length;
  81.         /* Starting i = 1 because first element is already max */
  82.         /*for (int i = 1; i < len; i++) {
  83.             if (max < matrix[row][i]) {
  84.                 max = matrix[row][i];
  85.             }
  86.         }
  87.         return max;
  88.         */
  89.         return Arrays.stream(matrix[row]).max().getAsDouble();
  90.     }
  91.  
  92.     public double maxElementAtColumn(int column) throws InvalidColumnNumberException {
  93.         /* Assuming the column is given starting from 1 and not 0 */
  94.         column -= 1;
  95.         if (column < 0 || column >= matrix[0].length) {
  96.             throw new InvalidColumnNumberException();
  97.         }
  98.         /*double max = matrix[0][column];
  99.         int len = matrix.length;
  100.         *//* Starting i = 1 because first element is already max *//*
  101.         for (int i = 1; i < len; i++) {
  102.             if (max < matrix[i][column]) {
  103.                 max = matrix[i][column];
  104.             }
  105.         }
  106.         return max;*/
  107.         final int columnAsFinal = column;
  108.         return Arrays.stream(matrix)
  109.                 .flatMapToDouble(row ->
  110.                         IntStream.range(0, row.length)
  111.                                 .filter(i -> i == columnAsFinal)
  112.                                 .mapToDouble(i -> row[i]))
  113.                 .max()
  114.                 .getAsDouble();
  115.     }
  116.  
  117.     public double sum() {
  118.         /*double sum = 0;
  119.         for (int i = matrix.length - 1; i >= 0; i--) {
  120.             for (int j = matrix[0].length - 1; j >= 0 ; j--) {
  121.                 sum += matrix[i][j];
  122.             }
  123.         }
  124.         return sum;*/
  125.         return Arrays.stream(matrix)
  126.                 .mapToDouble(row -> Arrays.stream(row).sum())
  127.                 .sum();
  128.     }
  129.  
  130.     public void swap(double a, double b) {
  131.         double temp = a;
  132.         a = b;
  133.         b = temp;
  134.     }
  135.  
  136.     public double[] toSortedArray() {
  137.         /*int width = matrix.length;
  138.         int height = matrix[0].length;
  139.         double result[] = new double[width * height];
  140.         *//* Populate the array with the matrix elements *//*
  141.         int k = 0;
  142.         for (int i = 0; i < width; i++) {
  143.             for (int j = 0; j < height ; j++) {
  144.                 result[k++] = matrix[i][j];
  145.             }
  146.         }
  147.         *//* Bubble Sort the result[] *//*
  148.         //System.out.println("Bubble Sort");
  149.         for (int i = 0; i < result.length-1; i++) {
  150.             for (int j = 0; j < result.length - i - 1; j++) {
  151.                 if (result[j] < result[j+1]) {
  152.                     //swap(result[j], result[j+1]);
  153.                     double temp = result[j];
  154.                     result[j] = result[j+1];
  155.                     result[j+1] = temp;
  156.                 }
  157.             }
  158.         }
  159.         return result;*/
  160.         return Arrays.stream(matrix)
  161.                 .flatMapToDouble(row -> Arrays.stream(row))
  162.                 .boxed()
  163.                 .sorted(Comparator.reverseOrder())
  164.                 .mapToDouble(i -> i.doubleValue())
  165.                 .toArray();
  166.     }
  167.  
  168.     @Override
  169.     public String toString() {
  170.         /*StringBuffer sb = new StringBuffer();
  171.         int width = matrix.length;
  172.         int height = matrix[0].length;
  173.         for (int i = 0; i < width; i++) {
  174.             for (int j = 0; j < height; j++) {
  175.                 sb.append(String.format("%.2f\t", matrix[i][j]));
  176.             }
  177.             sb.setLength(sb.length() - 1);
  178.             sb.append("\n");
  179.         }
  180.         sb.setLength(sb.length() - 1);
  181.         return sb.toString();*/
  182.  
  183.         return Arrays.stream(matrix)
  184.                 .map(row ->
  185.                         Arrays.stream(row)
  186.                                 .mapToObj(element -> String.format("%.2f", element))
  187.                                 .collect(Collectors.joining("\t"))
  188.                 )
  189.                 .collect(Collectors.joining("\n"));
  190.     }
  191.  
  192.     @Override
  193.     public int hashCode() {
  194.         final int prime = 31;
  195.         int result = 1;
  196.         result = prime * result + Arrays.deepHashCode(matrix);
  197.         return result;
  198.     }
  199.  
  200.     @Override
  201.     public boolean equals(Object obj) {
  202.         if (this == obj)
  203.             return true;
  204.         if (obj == null)
  205.             return false;
  206.         if (getClass() != obj.getClass())
  207.             return false;
  208.         DoubleMatrix other = (DoubleMatrix) obj;
  209.         if (!Arrays.deepEquals(matrix, other.matrix))
  210.             return false;
  211.         return true;
  212.     }
  213.  
  214. }
  215.  
  216. class MatrixReader {
  217.     public static DoubleMatrix read(InputStream input) throws InsufficientElementsException {
  218.         Scanner scanner = new Scanner(input);
  219.         int width = scanner.nextInt();
  220.         int height = scanner.nextInt();
  221.         int len = width * height;
  222.         double array[] = new double[len];
  223.         /*for (int i = 0; i < len; i++) {
  224.             a[i] = scan.nextDouble();
  225.         }*/
  226.         IntStream.range(0, len).forEach(i -> array[i] = scanner.nextDouble());
  227.         return new DoubleMatrix(array, width, height);
  228.     }
  229. }
  230.  
  231. public class DoubleMatrixTester {
  232.  
  233.     public static void main(String[] args) throws Exception {
  234.         Scanner scanner = new Scanner(System.in);
  235.  
  236.         int tests = scanner.nextInt();
  237.         DoubleMatrix fm = null;
  238.  
  239.         double[] info = null;
  240.  
  241.         DecimalFormat format = new DecimalFormat("0.00");
  242.  
  243.         for (int t = 0; t < tests; t++) {
  244.  
  245.             String operation = scanner.next();
  246.  
  247.             switch (operation) {
  248.                 case "READ": {
  249.                     int N = scanner.nextInt();
  250.                     int R = scanner.nextInt();
  251.                     int C = scanner.nextInt();
  252.  
  253.                     double[] f = new double[N];
  254.  
  255.                     for (int i = 0; i < f.length; i++)
  256.                         f[i] = scanner.nextDouble();
  257.  
  258.                     try {
  259.                         fm = new DoubleMatrix(f, R, C);
  260.                         info = Arrays.copyOf(f, f.length);
  261.  
  262.                     } catch (InsufficientElementsException e) {
  263.                         System.out.println("Exception caught: " + e.getMessage());
  264.                     }
  265.  
  266.                     break;
  267.                 }
  268.  
  269.                 case "INPUT_TEST": {
  270.                     int R = scanner.nextInt();
  271.                     int C = scanner.nextInt();
  272.  
  273.                     StringBuilder sb = new StringBuilder();
  274.  
  275.                     sb.append(R + " " + C + "\n");
  276.  
  277.                     scanner.nextLine();
  278.  
  279.                     for (int i = 0; i < R; i++)
  280.                         sb.append(scanner.nextLine() + "\n");
  281.  
  282.                     fm = MatrixReader.read(new ByteArrayInputStream(sb
  283.                             .toString().getBytes()));
  284.  
  285.                     info = new double[R * C];
  286.                     Scanner tempScanner = new Scanner(new ByteArrayInputStream(sb
  287.                             .toString().getBytes()));
  288.                     tempScanner.nextDouble();
  289.                     tempScanner.nextDouble();
  290.                     for (int z = 0; z < R * C; z++) {
  291.                         info[z] = tempScanner.nextDouble();
  292.                     }
  293.  
  294.                     tempScanner.close();
  295.  
  296.                     break;
  297.                 }
  298.  
  299.                 case "PRINT": {
  300.                     System.out.println(fm.toString());
  301.                     break;
  302.                 }
  303.  
  304.                 case "DIMENSION": {
  305.                     System.out.println("Dimensions: " + fm.getDimensions());
  306.                     break;
  307.                 }
  308.  
  309.                 case "COUNT_ROWS": {
  310.                     System.out.println("Rows: " + fm.rows());
  311.                     break;
  312.                 }
  313.  
  314.                 case "COUNT_COLUMNS": {
  315.                     System.out.println("Columns: " + fm.columns());
  316.                     break;
  317.                 }
  318.  
  319.                 case "MAX_IN_ROW": {
  320.                     int row = scanner.nextInt();
  321.                     try {
  322.                         System.out.println("Max in row: "
  323.                                 + format.format(fm.maxElementAtRow(row)));
  324.                     } catch (InvalidRowNumberException e) {
  325.                         System.out.println("Exception caught: " + e.getMessage());
  326.                     }
  327.                     break;
  328.                 }
  329.  
  330.                 case "MAX_IN_COLUMN": {
  331.                     int col = scanner.nextInt();
  332.                     try {
  333.                         System.out.println("Max in column: "
  334.                                 + format.format(fm.maxElementAtColumn(col)));
  335.                     } catch (InvalidColumnNumberException e) {
  336.                         System.out.println("Exception caught: " + e.getMessage());
  337.                     }
  338.                     break;
  339.                 }
  340.  
  341.                 case "SUM": {
  342.                     System.out.println("Sum: " + format.format(fm.sum()));
  343.                     break;
  344.                 }
  345.  
  346.                 case "CHECK_EQUALS": {
  347.                     int val = scanner.nextInt();
  348.  
  349.                     int maxOps = val % 7;
  350.  
  351.                     for (int z = 0; z < maxOps; z++) {
  352.                         double work[] = Arrays.copyOf(info, info.length);
  353.  
  354.                         int e1 = (31 * z + 7 * val + 3 * maxOps) % info.length;
  355.                         int e2 = (17 * z + 3 * val + 7 * maxOps) % info.length;
  356.  
  357.                         if (e1 > e2) {
  358.                             double temp = work[e1];
  359.                             work[e1] = work[e2];
  360.                             work[e2] = temp;
  361.                         }
  362.  
  363.                         DoubleMatrix f1 = fm;
  364.                         DoubleMatrix f2 = new DoubleMatrix(work, fm.rows(),
  365.                                 fm.columns());
  366.                         System.out
  367.                                 .println("Equals check 1: "
  368.                                         + f1.equals(f2)
  369.                                         + " "
  370.                                         + f2.equals(f1)
  371.                                         + " "
  372.                                         + (f1.hashCode() == f2.hashCode() && f1
  373.                                         .equals(f2)));
  374.                     }
  375.  
  376.                     if (maxOps % 2 == 0) {
  377.                         DoubleMatrix f1 = fm;
  378.                         DoubleMatrix f2 = new DoubleMatrix(new double[]{3.0, 5.0,
  379.                                 7.5}, 1, 1);
  380.  
  381.                         System.out
  382.                                 .println("Equals check 2: "
  383.                                         + f1.equals(f2)
  384.                                         + " "
  385.                                         + f2.equals(f1)
  386.                                         + " "
  387.                                         + (f1.hashCode() == f2.hashCode() && f1
  388.                                         .equals(f2)));
  389.                     }
  390.  
  391.                     break;
  392.                 }
  393.  
  394.                 case "SORTED_ARRAY": {
  395.                     double[] arr = fm.toSortedArray();
  396.  
  397.                     String arrayString = "[";
  398.  
  399.                     if (arr.length > 0)
  400.                         arrayString += format.format(arr[0]) + "";
  401.  
  402.                     for (int i = 1; i < arr.length; i++)
  403.                         arrayString += ", " + format.format(arr[i]);
  404.  
  405.                     arrayString += "]";
  406.  
  407.                     System.out.println("Sorted array: " + arrayString);
  408.                     break;
  409.                 }
  410.  
  411.             }
  412.  
  413.         }
  414.  
  415.         scanner.close();
  416.     }
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement