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