Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.ByteArrayInputStream;
- import java.text.DecimalFormat;
- import java.util.Arrays;
- import java.util.Scanner;
- 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();
- }
- }
- import java.util.Arrays;
- public final class FloatMatrix {
- final int m;
- final int n;
- final float[][] matrica;
- public FloatMatrix(float a[], int m, int n) throws InsufficientElementsException {
- if (a.length < m * n) {
- throw new InsufficientElementsException();
- }
- this.m = m;
- this.n = n;
- matrica = new float[m][n];
- float[] pom = new float[m*n];
- if(a.length> m*n)
- {
- for(int k=(a.length-(m*n)), h=0; k<a.length; k++, h++)
- {
- pom[h]=a[k];
- }
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- {
- matrica[i][j] = pom[i * n + j];
- }
- }
- }
- else
- {
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- {
- matrica[i][j] = a[i * n + j];
- }
- }
- }
- }
- public String getMatrixDimensions() {
- return String.format("[%d x %d]", m, n);
- }
- public int numRows() {
- return m;
- }
- public int numColumns() {
- return n;
- }
- public float maxElementAtRow(int row) throws InvalidRowNumberException {
- if (row < 1) {
- throw new InvalidRowNumberException();
- }
- if (row > numRows()) {
- throw new InvalidRowNumberException();
- }
- float max = Float.MIN_VALUE;
- for (int i = 0; i < numColumns(); i++) {
- if (max < matrica[row - 1][i]) {
- max = matrica[row - 1][i];
- }
- }
- return max;
- }
- public float maxElementAtColumn(int col) throws InvalidColumnNumberException {
- if (col < 1) {
- throw new InvalidColumnNumberException();
- }
- if(col > numColumns())
- {
- throw new InvalidColumnNumberException();
- }
- float max=Float.MIN_VALUE;
- for(int i=0; i<numRows(); ++i)
- {
- if(max<matrica[i][col - 1])
- {
- max=matrica[i][col - 1];
- }
- }
- return max;
- }
- public float sum() {
- float suma=0;
- for(int i=0; i<numRows(); i++) {
- for (int j = 0; j < numColumns(); j++)
- {
- suma += matrica[i][j];
- }
- }
- return suma;
- }
- public float[] toSortedArray() {
- float[] niza = new float[numColumns() * numRows()];
- for (int i = 0; i < numRows(); i++) {
- for (int j = 0; j < numColumns(); j++) {
- niza[i * numColumns() + j] = matrica[i][j];
- }
- }
- Arrays.sort(niza);
- for (int i = 0; i < niza.length / 2; i++) {
- float temp = niza[i];
- niza[i] = niza[niza.length - i - 1];
- niza[niza.length - i - 1] = temp;
- }
- return niza;
- }
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder(numColumns() * numRows() * 7 + numRows());
- for (int i = 0; i < numRows(); i++) {
- for (int j = 0; j < numColumns(); j++) {
- builder.append(String.format("%.2f", matrica[i][j]));
- builder.append('\t');
- }
- builder.deleteCharAt(builder.lastIndexOf("\t"));
- builder.append('\n');
- }
- builder.deleteCharAt(builder.lastIndexOf("\n"));
- return builder.toString();
- }
- @Override
- public boolean equals(Object o) {
- if (o == null) {
- return false;
- }
- if (!(o instanceof FloatMatrix)) {
- return false;
- }
- FloatMatrix f = (FloatMatrix) o;
- if (this.numRows() != f.numRows()) {
- return false;
- }
- if (this.numColumns() != f.numColumns()) {
- return false;
- }
- for (int i = 0; i < numRows(); i++) {
- for (int j = 0; j < numColumns(); j++) {
- if (matrica[i][j] != f.matrica[i][j]) {
- return false;
- }
- }
- }
- return true;
- }
- // PRAVILOTO: AKO dvata hashCode() na razlicni objekti se ISTI, TOGAS mora equals() na dvata objekti da vrakaja TRUE
- private int hashValue = 0;
- private boolean hasHashValue = false;
- @Override
- public int hashCode() {
- if (hasHashValue) {
- return hashValue;
- }
- hashValue = 0;
- for (int i = 0; i < numRows(); i++) {
- for (int j = 0; j < numColumns(); j++) {
- hashValue ^= (int) matrica[i][j];
- }
- }
- return hashValue;
- }
- }
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- public class MatrixReader {
- public static FloatMatrix readFloatMatrix(InputStream input) throws InsufficientElementsException, IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- String[] dimensions = reader.readLine().split(" ");
- int m = Integer.parseInt(dimensions[0]);
- int n = Integer.parseInt(dimensions[1]);
- float[] a = new float[m * n];
- for (int i = 0; i < m; i++) {
- String[] elements = reader.readLine().split(" ");
- for (int j = 0; j < n; j++) {
- a[i * n + j] = Float.parseFloat(elements[j]);
- }
- }
- return new FloatMatrix(a, m, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement