Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. import java.text.DecimalFormat;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.InputStream;
  6.  
  7. final class DoubleMatrix{
  8. final double[][] doubleMatrix;
  9. final int m;
  10. final int n;
  11.  
  12. public DoubleMatrix(double a[], int m, int n) throws InsufficientElementsException{
  13. this.m=m;
  14. this.n=n;
  15.  
  16. this.doubleMatrix=new double[m][n];
  17. if(m*n > a.length){
  18. throw new InsufficientElementsException("Insufficient number of elements");
  19. }
  20. int k=a.length-1;
  21. for(int i=m-1; i>=0;i--){
  22. for(int j=n-1; j>=0;j--){
  23. doubleMatrix[i][j]=a[k];
  24. k--;
  25. }
  26. }
  27. }
  28. public String getDimensions(){
  29. return String.format("[%d x %d]",m,n);
  30. }
  31. public int rows(){
  32. return m;
  33. }
  34. public int columns(){
  35. return n;
  36. }
  37. public double maxElementAtRow(int row) throws InvalidRowNumberException{
  38. int index=row-1;
  39. if(index<0||index>=m){
  40. throw new InvalidRowNumberException("Invalid row number");
  41. }
  42. else{
  43. double max=doubleMatrix[index][0];
  44. for(int i=0;i<columns();i++){
  45. if(doubleMatrix[index][i]>max){
  46. max=doubleMatrix[index][i];
  47. }
  48. }
  49. return max;
  50. }
  51. }
  52. public double maxElementAtColumn(int column) throws InvalidColumnNumberException{
  53. int index=column-1;
  54. if(index<0||index>=n){
  55. throw new InvalidColumnNumberException("Invalid column number");
  56. }
  57. else{
  58. double max=doubleMatrix[0][index];
  59. for(int i=0;i<rows();i++){
  60. if(doubleMatrix[i][index]>max){
  61. max=doubleMatrix[i][index];
  62. }
  63. }
  64. return max;
  65. }
  66. }
  67. public double sum(){
  68. double suma=0;
  69. for(int i=0;i<m;i++){
  70. for(int j=0;j<n;j++){
  71. suma+=doubleMatrix[i][j];
  72. }
  73. }
  74. return suma;
  75. }
  76. public double[] toSortedArray(){
  77. int brojac=0;
  78. double[] array=new double[m*n];
  79. for(int i=0;i<doubleMatrix.length;i++){
  80. for(int j=0;j<doubleMatrix[i].length;j++){
  81. array[brojac]=doubleMatrix[i][j];
  82. brojac++;
  83. }
  84. }
  85. Arrays.sort(array);
  86. brojac=0;
  87. double[] array1=new double[array.length];
  88. for(int i=array.length-1;i>=0;i--){
  89. array1[brojac]=array[i];
  90. brojac++;
  91. }
  92. return array1;
  93. }
  94.  
  95.  
  96. @Override
  97. public int hashCode() {
  98. final int prime = 31;
  99. int result = 1;
  100. result = prime * result + Arrays.deepHashCode(doubleMatrix);
  101. result = prime * result + m;
  102. result = prime * result + n;
  103. return result;
  104. }
  105. @Override
  106. public boolean equals(Object obj) {
  107. if (this == obj)
  108. return true;
  109. if (obj == null)
  110. return false;
  111. if (getClass() != obj.getClass())
  112. return false;
  113. DoubleMatrix other = (DoubleMatrix) obj;
  114. if (!Arrays.deepEquals(doubleMatrix, other.doubleMatrix))
  115. return false;
  116. if (m != other.m)
  117. return false;
  118. if (n != other.n)
  119. return false;
  120. return true;
  121. }
  122. @Override
  123. public String toString() {
  124. StringBuilder sb=new StringBuilder();
  125. for(int i=0;i<doubleMatrix.length;i++){
  126. for(int j=0;j<doubleMatrix[i].length;j++){
  127. sb.append(String.format("%.2f", doubleMatrix[i][j]));
  128. if(j!=columns()-1){
  129. sb.append("\t");
  130. }
  131. }
  132. if(i!=rows()-1){
  133. sb.append("\n");
  134. }
  135. }
  136. return sb.toString();
  137. }
  138.  
  139. }
  140.  
  141. class InsufficientElementsException extends Exception{
  142. public InsufficientElementsException(String message){
  143. super(message);
  144. }
  145. }
  146. class InvalidRowNumberException extends Exception{
  147. public InvalidRowNumberException (String message){
  148. super(message);
  149. }
  150. }
  151. class InvalidColumnNumberException extends Exception{
  152. public InvalidColumnNumberException (String message){
  153. super(message);
  154. }
  155. }
  156. class MatrixReader {
  157. public static DoubleMatrix read(InputStream input) throws InsufficientElementsException{
  158. Scanner sc=new Scanner(input);
  159. int row=sc.nextInt();
  160. int column=sc.nextInt();
  161. double[] niza=new double[row*column];
  162. for(int i=0;i<niza.length;i++){
  163. niza[i]=sc.nextDouble();
  164. }
  165. sc.close();
  166. DoubleMatrix matrix=new DoubleMatrix(niza,row,column);
  167. return matrix;
  168. }
  169. }
  170.  
  171.  
  172. public class DoubleMatrixTester {
  173.  
  174. public static void main(String[] args) throws Exception {
  175. Scanner scanner = new Scanner(System.in);
  176.  
  177. int tests = scanner.nextInt();
  178. DoubleMatrix fm = null;
  179.  
  180. double[] info = null;
  181.  
  182. DecimalFormat format = new DecimalFormat("0.00");
  183.  
  184. for (int t = 0; t < tests; t++) {
  185.  
  186. String operation = scanner.next();
  187.  
  188. switch (operation) {
  189. case "READ": {
  190. int N = scanner.nextInt();
  191. int R = scanner.nextInt();
  192. int C = scanner.nextInt();
  193.  
  194. double[] f = new double[N];
  195.  
  196. for (int i = 0; i < f.length; i++)
  197. f[i] = scanner.nextDouble();
  198.  
  199. try {
  200. fm = new DoubleMatrix(f, R, C);
  201. info = Arrays.copyOf(f, f.length);
  202.  
  203. } catch (InsufficientElementsException e) {
  204. System.out.println("Exception caught: " + e.getMessage());
  205. }
  206.  
  207. break;
  208. }
  209.  
  210. case "INPUT_TEST": {
  211. int R = scanner.nextInt();
  212. int C = scanner.nextInt();
  213.  
  214. StringBuilder sb = new StringBuilder();
  215.  
  216. sb.append(R + " " + C + "\n");
  217.  
  218. scanner.nextLine();
  219.  
  220. for (int i = 0; i < R; i++)
  221. sb.append(scanner.nextLine() + "\n");
  222.  
  223. fm = MatrixReader.read(new ByteArrayInputStream(sb
  224. .toString().getBytes()));
  225.  
  226. info = new double[R * C];
  227. Scanner tempScanner = new Scanner(new ByteArrayInputStream(sb
  228. .toString().getBytes()));
  229. tempScanner.nextDouble();
  230. tempScanner.nextDouble();
  231. for (int z = 0; z < R * C; z++) {
  232. info[z] = tempScanner.nextDouble();
  233. }
  234.  
  235. tempScanner.close();
  236.  
  237. break;
  238. }
  239.  
  240. case "PRINT": {
  241. System.out.println(fm.toString());
  242. break;
  243. }
  244.  
  245. case "DIMENSION": {
  246. System.out.println("Dimensions: " + fm.getDimensions());
  247. break;
  248. }
  249.  
  250. case "COUNT_ROWS": {
  251. System.out.println("Rows: " + fm.rows());
  252. break;
  253. }
  254.  
  255. case "COUNT_COLUMNS": {
  256. System.out.println("Columns: " + fm.columns());
  257. break;
  258. }
  259.  
  260. case "MAX_IN_ROW": {
  261. int row = scanner.nextInt();
  262. try {
  263. System.out.println("Max in row: "
  264. + format.format(fm.maxElementAtRow(row)));
  265. } catch (InvalidRowNumberException e) {
  266. System.out.println("Exception caught: " + e.getMessage());
  267. }
  268. break;
  269. }
  270.  
  271. case "MAX_IN_COLUMN": {
  272. int col = scanner.nextInt();
  273. try {
  274. System.out.println("Max in column: "
  275. + format.format(fm.maxElementAtColumn(col)));
  276. } catch (InvalidColumnNumberException e) {
  277. System.out.println("Exception caught: " + e.getMessage());
  278. }
  279. break;
  280. }
  281.  
  282. case "SUM": {
  283. System.out.println("Sum: " + format.format(fm.sum()));
  284. break;
  285. }
  286.  
  287. case "CHECK_EQUALS": {
  288. int val = scanner.nextInt();
  289.  
  290. int maxOps = val % 7;
  291.  
  292. for (int z = 0; z < maxOps; z++) {
  293. double work[] = Arrays.copyOf(info, info.length);
  294.  
  295. int e1 = (31 * z + 7 * val + 3 * maxOps) % info.length;
  296. int e2 = (17 * z + 3 * val + 7 * maxOps) % info.length;
  297.  
  298. if (e1 > e2) {
  299. double temp = work[e1];
  300. work[e1] = work[e2];
  301. work[e2] = temp;
  302. }
  303.  
  304. DoubleMatrix f1 = fm;
  305. DoubleMatrix f2 = new DoubleMatrix(work, fm.rows(),
  306. fm.columns());
  307. System.out
  308. .println("Equals check 1: "
  309. + f1.equals(f2)
  310. + " "
  311. + f2.equals(f1)
  312. + " "
  313. + (f1.hashCode() == f2.hashCode()&&f1
  314. .equals(f2)));
  315. }
  316.  
  317. if (maxOps % 2 == 0) {
  318. DoubleMatrix f1 = fm;
  319. DoubleMatrix f2 = new DoubleMatrix(new double[]{3.0, 5.0,
  320. 7.5}, 1, 1);
  321.  
  322. System.out
  323. .println("Equals check 2: "
  324. + f1.equals(f2)
  325. + " "
  326. + f2.equals(f1)
  327. + " "
  328. + (f1.hashCode() == f2.hashCode()&&f1
  329. .equals(f2)));
  330. }
  331.  
  332. break;
  333. }
  334.  
  335. case "SORTED_ARRAY": {
  336. double[] arr = fm.toSortedArray();
  337.  
  338. String arrayString = "[";
  339.  
  340. if (arr.length > 0)
  341. arrayString += format.format(arr[0]) + "";
  342.  
  343. for (int i = 1; i < arr.length; i++)
  344. arrayString += ", " + format.format(arr[i]);
  345.  
  346. arrayString += "]";
  347.  
  348. System.out.println("Sorted array: " + arrayString);
  349. break;
  350. }
  351.  
  352. }
  353.  
  354. }
  355.  
  356. scanner.close();
  357. }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement