Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Reverse {
- // :NOTE: numStr? -- maybe you want to name it "row" or "line", String is a class in Java?
- public static int[][] addLineInMatrix(int[][] matrixInput, int numStr) {
- if (matrixInput.length == numStr + 1) {
- int[][] matrixOutput = new int[matrixInput.length * 2][];
- for (int i = 0; i < matrixInput.length; i++) {
- matrixOutput[i] = new int[matrixInput[i].length];
- System.arraycopy(matrixInput[i], 0, matrixOutput[i], 0, matrixInput[i].length);
- }
- Arrays.fill(matrixOutput, matrixInput.length, matrixInput.length*2, new int[1]);
- matrixInput = matrixOutput;
- }
- return matrixInput;
- }
- public static int[] addDigitInMatrix(int[] arrayInput, int numDig) {
- if (arrayInput.length == numDig + 1) {
- int[] arrayOutput = new int[arrayInput.length * 2];
- System.arraycopy(arrayInput, 0, arrayOutput, 0, arrayInput.length);
- arrayInput = arrayOutput;
- }
- return arrayInput;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int[][] matrix = new int[1][1];
- int[] matrixSize = new int[1];
- // :NOTE: same - naming
- int numStr = 0;
- int numElem = 0;
- while (in.hasNextLine()) {
- Scanner str = new Scanner(in.nextLine());
- while (str.hasNextInt()) {
- matrix[numStr][numElem] = str.nextInt();
- matrix[numStr] = addDigitInMatrix(matrix[numStr], numElem);
- numElem++;
- }
- matrix = addLineInMatrix(matrix, numStr);
- matrixSize = addDigitInMatrix(matrixSize, numStr);
- numStr++;
- matrixSize[numStr - 1] = numElem;
- numElem = 0;
- }
- in.close();
- for (int i = numStr - 1; i > -1; i--) {
- for (int j = matrixSize[i] - 1; j > -1; j--) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement