Advertisement
Guest User

Untitled

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