Advertisement
alyoshinkkaa

Untitled

Jul 29th, 2023 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Reverse {
  5.     // :NOTE: naming -- it's better to name "matrix" not "mass", do not abbreviate words incomprehensibly
  6.     public static int[][] addLineInMass(int[][] mass, int numStr) {
  7.         // :NOTE: same -- naming
  8.         if (mass.length == numStr + 1) {
  9.             // :NOTE: mass1? -- not understandable name
  10.             int[][] mass1 = new int[mass.length * 2][];
  11.             for (int i = 0; i < mass.length; i++) {
  12.                 mass1[i] = new int[mass[i].length];
  13.                 System.arraycopy(mass[i], 0, mass1[i], 0, mass[i].length);
  14.             }
  15.             Arrays.fill(mass1, mass.length, mass.length*2, new int[1]);
  16.             mass = mass1;
  17.         }
  18.             return mass;
  19.     }
  20.     // :NOTE: same -- naming
  21.     public static int[] addDigitInMass(int[] mass, int numDig) {
  22.         if (mass.length == numDig + 1) {
  23.             // :NOTE: same -- naming
  24.             int[] mass1 = new int[mass.length * 2];
  25.             System.arraycopy(mass, 0, mass1, 0, mass.length);
  26.             mass = mass1;
  27.         }
  28.         return mass;
  29.     }
  30.     public static void main(String[] args) {
  31.         Scanner in = new Scanner(System.in);
  32.         // :NOTE: same -- naming (massiv is not a word from English)
  33.         int[][] mass = new int[1][1];
  34.         // :NOTE: same -- naming
  35.         int[] massSize = new int[1];
  36.         int numStr = 0;
  37.         int numElem = 0;
  38.         while (in.hasNextLine()) {
  39.             Scanner str = new Scanner(in.nextLine());
  40.             while (str.hasNextInt()) {
  41.                 mass[numStr][numElem] = str.nextInt();
  42.                 mass[numStr] = addDigitInMass(mass[numStr], numElem);
  43.                 numElem++;
  44.             }
  45.             mass = addLineInMass(mass, numStr);
  46.             massSize = addDigitInMass(massSize, numStr);
  47.             numStr++;
  48.             massSize[numStr - 1] = numElem;
  49.             numElem = 0;
  50.         }
  51.         // :NOTE: your scanner is not used in the next "for" block of code, so you should close it here
  52.         for (int i = numStr - 1; i > -1; i--) {
  53.             for (int j = massSize[i] - 1; j > -1; j--) {
  54.                 System.out.print(mass[i][j] + " ");
  55.             }
  56.             System.out.println();
  57.         }
  58.         in.close();
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement