Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- import java.text.DecimalFormat;
- import java.io.ByteArrayInputStream;
- import java.io.InputStream;
- final class DoubleMatrix{
- final double[][] doubleMatrix;
- final int m;
- final int n;
- public DoubleMatrix(double a[], int m, int n) throws InsufficientElementsException{
- this.m=m;
- this.n=n;
- this.doubleMatrix=new double[m][n];
- if(m*n > a.length){
- throw new InsufficientElementsException("Insufficient number of elements");
- }
- int k=a.length-1;
- for(int i=m-1; i>=0;i--){
- for(int j=n-1; j>=0;j--){
- doubleMatrix[i][j]=a[k];
- k--;
- }
- }
- }
- public String getDimensions(){
- return String.format("[%d x %d]",m,n);
- }
- public int rows(){
- return m;
- }
- public int columns(){
- return n;
- }
- public double maxElementAtRow(int row) throws InvalidRowNumberException{
- int index=row-1;
- if(index<0||index>=m){
- throw new InvalidRowNumberException("Invalid row number");
- }
- else{
- double max=doubleMatrix[index][0];
- for(int i=0;i<columns();i++){
- if(doubleMatrix[index][i]>max){
- max=doubleMatrix[index][i];
- }
- }
- return max;
- }
- }
- public double maxElementAtColumn(int column) throws InvalidColumnNumberException{
- int index=column-1;
- if(index<0||index>=n){
- throw new InvalidColumnNumberException("Invalid column number");
- }
- else{
- double max=doubleMatrix[0][index];
- for(int i=0;i<rows();i++){
- if(doubleMatrix[i][index]>max){
- max=doubleMatrix[i][index];
- }
- }
- return max;
- }
- }
- public double sum(){
- double suma=0;
- for(int i=0;i<m;i++){
- for(int j=0;j<n;j++){
- suma+=doubleMatrix[i][j];
- }
- }
- return suma;
- }
- public double[] toSortedArray(){
- int brojac=0;
- double[] array=new double[m*n];
- for(int i=0;i<doubleMatrix.length;i++){
- for(int j=0;j<doubleMatrix[i].length;j++){
- array[brojac]=doubleMatrix[i][j];
- brojac++;
- }
- }
- Arrays.sort(array);
- brojac=0;
- double[] array1=new double[array.length];
- for(int i=array.length-1;i>=0;i--){
- array1[brojac]=array[i];
- brojac++;
- }
- return array1;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + Arrays.deepHashCode(doubleMatrix);
- result = prime * result + m;
- 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.deepEquals(doubleMatrix, other.doubleMatrix))
- return false;
- if (m != other.m)
- return false;
- if (n != other.n)
- return false;
- return true;
- }
- @Override
- public String toString() {
- StringBuilder sb=new StringBuilder();
- for(int i=0;i<doubleMatrix.length;i++){
- for(int j=0;j<doubleMatrix[i].length;j++){
- sb.append(String.format("%.2f", doubleMatrix[i][j]));
- if(j!=columns()-1){
- sb.append("\t");
- }
- }
- if(i!=rows()-1){
- sb.append("\n");
- }
- }
- return sb.toString();
- }
- }
- class InsufficientElementsException extends Exception{
- public InsufficientElementsException(String message){
- super(message);
- }
- }
- class InvalidRowNumberException extends Exception{
- public InvalidRowNumberException (String message){
- super(message);
- }
- }
- class InvalidColumnNumberException extends Exception{
- public InvalidColumnNumberException (String message){
- super(message);
- }
- }
- class MatrixReader {
- public static DoubleMatrix read(InputStream input) throws InsufficientElementsException{
- Scanner sc=new Scanner(input);
- int row=sc.nextInt();
- int column=sc.nextInt();
- double[] niza=new double[row*column];
- for(int i=0;i<niza.length;i++){
- niza[i]=sc.nextDouble();
- }
- sc.close();
- DoubleMatrix matrix=new DoubleMatrix(niza,row,column);
- return matrix;
- }
- }
- 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