Advertisement
lameski

Matrica od realni broevi

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