Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.ByteArrayInputStream;
- import java.io.InputStream;
- import java.text.DecimalFormat;
- import java.time.format.ResolverStyle;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- class InsufficientElementsException extends Exception
- {
- public InsufficientElementsException()
- {
- super(String.format("Insufficient number of elements"));
- }
- }
- class InvalidRowNumberException extends Exception
- {
- public InvalidRowNumberException ()
- {
- super(String.format("Invalid row number")); }
- }
- class InvalidColumnNumberException extends Exception
- {
- public InvalidColumnNumberException ()
- {
- super(String.format("Invalid column number")); }
- }
- final class DoubleMatrix
- {
- private double a[];
- private int m, n;
- private double matrix[][];
- public DoubleMatrix(double a[], int m, int n) throws InsufficientElementsException
- {
- super();
- this.m = m;
- this.n = n;
- this.matrix = new double[m][n];
- if(m*n>a.length)
- throw new InsufficientElementsException();
- else if (m*n==a.length)
- {
- int counter = 0;
- for(int i=0; i<m; i++)
- {
- for(int j=0; j<n; j++)
- {
- this.matrix[i][j] = a[counter];
- counter++;
- }
- }
- }
- else
- {
- int counter = a.length-m*n;
- //System.out.println(counter);
- for(int i=0; i<m; i++)
- {
- for(int j=0; j<n; j++)
- {
- this.matrix[i][j] = a[counter];
- // System.out.println(matrix[i][j]);
- counter++;
- }
- }
- }
- }
- public String getDimensions()
- {
- return "["+m+" x "+n + "]";
- }
- public int rows()
- {
- return m;
- }
- public int columns()
- {
- return n;
- }
- public double maxElementAtRow(int row) throws InvalidRowNumberException
- {
- if(row>m || row<1)
- throw new InvalidRowNumberException();
- double max =-999999;
- for(int i=0; i<n; i++)
- {
- if(matrix[row-1][i]>max)
- max = matrix[row-1][i];
- }
- return max;
- }
- public double maxElementAtColumn(int column) throws InvalidColumnNumberException
- {
- if(column>n || column<1)
- throw new InvalidColumnNumberException();
- double max =-999999;
- for(int i=0; i<m; i++)
- {
- if(matrix[i][column-1]>max)
- max = matrix[i][column-1];
- }
- return max;
- }
- public double sum()
- {
- double sum = 0.0;
- for(int i=0; i<m; i++)
- for(int j=0; j<n; j++)
- {
- sum+=matrix[i][j];
- }
- return sum;
- }
- public double[] toSortedArray()
- {
- double[] arr = new double[m*n];
- int k=0;
- for(int i=0; i<m; i++)
- {
- for(int j=0; j<n; j++)
- {
- arr[k] = matrix[i][j];
- k++;
- }
- }
- for(int i=0; i<m*n-1; i++)
- {
- for(int j=i; j<m*n; j++)
- {
- if(arr[i]<arr[j])
- {
- double temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- return arr;
- }
- @Override
- public String toString() {
- String s = "";
- for(int i=0; i<m-1; i++)
- {
- for(int j=0; j<n-1; j++)
- {
- s += String.format("%.2f\t", matrix[i][j]);
- }
- s+= String.format("%.2f\n", matrix[i][n-1]);
- }
- for(int i=0; i<n-1; i++)
- s+= String.format("%.2f\t", matrix[m-1][i]);
- s+= String.format("%.2f", matrix[m-1][n-1]);
- return s;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + Arrays.hashCode(a);
- result = prime * result + m;
- result = prime * result + Arrays.deepHashCode(matrix);
- result = prime * result + n;
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- DoubleMatrix other = (DoubleMatrix) obj;
- if (!Arrays.equals(a, other.a))
- return false;
- if (m != other.m)
- return false;
- if (!Arrays.deepEquals(matrix, other.matrix))
- return false;
- if (n != other.n)
- return false;
- return true;
- }
- }
- class MatrixReader{
- public static DoubleMatrix read(InputStream input) throws InsufficientElementsException
- {
- Scanner in = new Scanner(input);
- int m = in.nextInt();
- int n = in.nextInt();
- double a[] = new double[m*n];
- int i=0;
- while(in.hasNextDouble())
- // for(int j=1; j<=n; j++)
- {
- a[i++] = in.nextDouble();
- }
- return new DoubleMatrix(a, m, n);
- }
- }
- public class DoubleMatrixTester {
- public static void main(String[] args) throws Exception {
- Scanner scanner = new Scanner(System.in);
- int tests = scanner.nextInt();
- DoubleMatrix fm = null;
- double[] info = null;
- DecimalFormat format = new DecimalFormat("0.00");
- for (int t = 0; t < tests; t++) {
- String operation = scanner.next();
- switch (operation) {
- case "READ": {
- int N = scanner.nextInt();
- int R = scanner.nextInt();
- int C = scanner.nextInt();
- double[] f = new double[N];
- for (int i = 0; i < f.length; i++)
- f[i] = scanner.nextDouble();
- try {
- fm = new DoubleMatrix(f, R, C);
- info = Arrays.copyOf(f, f.length);
- } catch (InsufficientElementsException e) {
- System.out.println("Exception caught: " + e.getMessage());
- }
- break;
- }
- case "INPUT_TEST": {
- int R = scanner.nextInt();
- int C = scanner.nextInt();
- StringBuilder sb = new StringBuilder();
- sb.append(R + " " + C + "\n");
- scanner.nextLine();
- for (int i = 0; i < R; i++)
- sb.append(scanner.nextLine() + "\n");
- fm = MatrixReader.read(new ByteArrayInputStream(sb
- .toString().getBytes()));
- info = new double[R * C];
- Scanner tempScanner = new Scanner(new ByteArrayInputStream(sb
- .toString().getBytes()));
- tempScanner.nextDouble();
- tempScanner.nextDouble();
- for (int z = 0; z < R * C; z++) {
- info[z] = tempScanner.nextDouble();
- }
- tempScanner.close();
- break;
- }
- case "PRINT": {
- System.out.println(fm.toString());
- break;
- }
- case "DIMENSION": {
- System.out.println("Dimensions: " + fm.getDimensions());
- break;
- }
- case "COUNT_ROWS": {
- System.out.println("Rows: " + fm.rows());
- break;
- }
- case "COUNT_COLUMNS": {
- System.out.println("Columns: " + fm.columns());
- break;
- }
- case "MAX_IN_ROW": {
- int row = scanner.nextInt();
- try {
- System.out.println("Max in row: "
- + format.format(fm.maxElementAtRow(row)));
- } catch (InvalidRowNumberException e) {
- System.out.println("Exception caught: " + e.getMessage());
- }
- break;
- }
- case "MAX_IN_COLUMN": {
- int col = scanner.nextInt();
- try {
- System.out.println("Max in column: "
- + format.format(fm.maxElementAtColumn(col)));
- } catch (InvalidColumnNumberException e) {
- System.out.println("Exception caught: " + e.getMessage());
- }
- break;
- }
- case "SUM": {
- System.out.println("Sum: " + format.format(fm.sum()));
- break;
- }
- case "CHECK_EQUALS": {
- int val = scanner.nextInt();
- int maxOps = val % 7;
- for (int z = 0; z < maxOps; z++) {
- double work[] = Arrays.copyOf(info, info.length);
- int e1 = (31 * z + 7 * val + 3 * maxOps) % info.length;
- int e2 = (17 * z + 3 * val + 7 * maxOps) % info.length;
- if (e1 > e2) {
- double temp = work[e1];
- work[e1] = work[e2];
- work[e2] = temp;
- }
- DoubleMatrix f1 = fm;
- DoubleMatrix f2 = new DoubleMatrix(work, fm.rows(),
- fm.columns());
- System.out
- .println("Equals check 1: "
- + f1.equals(f2)
- + " "
- + f2.equals(f1)
- + " "
- + (f1.hashCode() == f2.hashCode()&&f1
- .equals(f2)));
- }
- if (maxOps % 2 == 0) {
- DoubleMatrix f1 = fm;
- DoubleMatrix f2 = new DoubleMatrix(new double[]{3.0, 5.0,
- 7.5}, 1, 1);
- System.out
- .println("Equals check 2: "
- + f1.equals(f2)
- + " "
- + f2.equals(f1)
- + " "
- + (f1.hashCode() == f2.hashCode() && f1
- .equals(f2)));
- }
- break;
- }
- case "SORTED_ARRAY": {
- double[] arr = fm.toSortedArray();
- String arrayString = "[";
- if (arr.length > 0)
- arrayString += format.format(arr[0]) + "";
- for (int i = 1; i < arr.length; i++)
- arrayString += ", " + format.format(arr[i]);
- arrayString += "]";
- System.out.println("Sorted array: " + arrayString);
- break;
- }
- }
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement