Advertisement
Guest User

Popraeno

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