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.util.Arrays;
- import java.util.Scanner;
- final class FloatMatrix {
- private float[][] a;
- private int m;
- private int n;
- public FloatMatrix(float[] a,int m, int n) throws InsufficientElementsException
- {
- if(a.length<m*n)
- throw new InsufficientElementsException();
- else if(a.length>m*n)
- {
- int s=a.length-m*n;
- int j=0;
- float[] b;
- b=new float[m*n];
- for(int i=s;i<a.length;i++)
- {
- b[j]=a[i];
- j++;
- }
- this.a=new float[m][n];
- this.m=m;
- this.n=n;
- int k=0;
- for(int i=0;i<m;i++)
- {
- for(int l=0;l<n;l++)
- {
- this.a[i][l]=b[k];
- k++;
- }
- }
- }
- else
- {
- this.a=new float[m][n];
- this.m=m;
- this.n=n;
- int k=0;
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n;j++)
- {
- this.a[i][j]=a[k];
- k++;
- }
- }
- }
- }
- public String getMatrixDimensions()
- {
- StringBuilder a=new StringBuilder();
- a.append("[ "+this.m+" x "+this.n+" ]");
- return a.toString();
- }
- public int numRows()
- {
- return this.m;
- }
- public int numColumns()
- {
- return this.n;
- }
- public float maxElementAtRow(int row) throws InvalidRowNumberException
- {
- if(row<1 || row>m)
- throw new InvalidRowNumberException();
- float[] b=new float[this.n];
- for(int i=0;i<n;i++)
- {
- b[i]=this.a[row-1][i];
- }
- float max=0;
- int flag=0;
- for(int i=0;i<b.length;i++)
- {
- if(b[i]>max)
- {max=b[i];
- flag=i;
- }
- }
- return b[flag];
- }
- public float maxElementAtColumn(int column) throws InvalidColumnNumberException
- {
- if(column<1 || column>n)
- throw new InvalidColumnNumberException();
- float max=a[0][column-1];
- for(int i=0;i<m;i++)
- {
- if(a[i][column-1]>max)
- max=a[i][column-1];
- }
- return max;
- }
- public float sum()
- {
- float sum=0;
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n;j++)
- {
- sum+=a[i][j];
- }
- }
- return sum;
- }
- public float[] toSortedArray()
- {
- float[] b=new float[m*n];
- int k=0;
- for(int i=0;i<m;i++)
- {
- for(int j=0;j<n;j++)
- {
- b[k]=a[i][j];
- k++;
- }
- }
- for(int i=0;i<k;i++)
- {
- for(int j=i+1;j<k;j++)
- {
- if(b[i]<b[j])
- {float tmp=b[i];
- b[i]=b[j];
- b[j]=tmp;}
- }
- }
- return b;
- }
- public String toString()
- {
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- {
- str.append(String.format("%.2f\t", a[i][j]));
- }
- if (i == m-1) break;
- str.append("\n");
- }
- return str.toString();
- }
- }
- class InsufficientElementsException extends Exception
- {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public InsufficientElementsException(String a)
- {
- super(a);
- }
- public InsufficientElementsException()
- {
- super("Insufficient number of elements");
- }
- }
- class InvalidRowNumberException extends Exception
- {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public InvalidRowNumberException()
- {
- super("Invalid row number");
- }
- }
- class InvalidColumnNumberException extends Exception
- {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public InvalidColumnNumberException()
- {
- super("Invalid column number");
- }
- }
- class MatrixReader {
- public static FloatMatrix readFloatMatrix(InputStream input) throws InsufficientElementsException
- {
- Scanner inn=new Scanner(input);
- int m=inn.nextInt();
- int n=inn.nextInt();
- float []arr = new float[m*n];
- for(int i=0;i<(m*n);i++){
- arr[i]=inn.nextFloat();
- }
- inn.close();
- FloatMatrix ianew=new FloatMatrix(arr,m,n);
- return ianew;
- }
- }
- /**
- * @author Kiko
- *
- */
- public class FloatMatrixTester {
- public static void main(String[] args) throws Exception {
- Scanner scanner = new Scanner(System.in);
- int tests = scanner.nextInt();
- FloatMatrix fm = null;
- float[] 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();
- float[] f = new float[N];
- for (int i = 0; i < f.length; i++)
- f[i] = scanner.nextFloat();
- try {
- fm = new FloatMatrix(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.readFloatMatrix(new ByteArrayInputStream(sb
- .toString().getBytes()));
- info = new float[R * C];
- Scanner tempScanner = new Scanner(new ByteArrayInputStream(sb
- .toString().getBytes()));
- tempScanner.nextFloat();
- tempScanner.nextFloat();
- for (int z = 0; z < R * C; z++) {
- info[z] = tempScanner.nextFloat();
- }
- tempScanner.close();
- break;
- }
- case "PRINT": {
- System.out.println(fm.toString());
- break;
- }
- case "DIMENSION": {
- System.out.println("Dimensions: " + fm.getMatrixDimensions());
- break;
- }
- case "COUNT_ROWS": {
- System.out.println("Rows: " + fm.numRows());
- break;
- }
- case "COUNT_COLUMNS": {
- System.out.println("Columns: " + fm.numColumns());
- 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++) {
- float 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) {
- float temp = work[e1];
- work[e1] = work[e2];
- work[e2] = temp;
- }
- FloatMatrix f1 = fm;
- FloatMatrix f2 = new FloatMatrix(work, fm.numRows(),
- fm.numColumns());
- System.out
- .println("Equals check 1: "
- + f1.equals(f2)
- + " "
- + f2.equals(f1)
- + " "
- + (f1.hashCode() == f2.hashCode() && f1
- .equals(f2)));
- }
- if (maxOps % 2 == 0) {
- FloatMatrix f1 = fm;
- FloatMatrix f2 = new FloatMatrix(new float[] { 3.0f, 5.0f,
- 7.5f }, 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": {
- float[] 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