Advertisement
alyoshinkkaa

Untitled

Jul 29th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ReverseAvg {
  5.     // :NOTE: naming -- it's better to name "matrix" not "mass", do not abbreviate words incomprehensibly
  6.     public static long[][] addLineInMass(long[][] mass, int numStr) {
  7.     // :NOTE: same -- naming
  8.         if (mass.length == numStr + 1) {
  9.             // :NOTE: mass1? -- not understandable name
  10.             long[][] mass1 = new long[mass.length * 2][];
  11.             for (int i = 0; i < mass.length; i++) {
  12.                 mass1[i] = new long[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 long[1]);
  16.             mass = mass1;
  17.         }
  18.         return mass;
  19.     }
  20.     // :NOTE: same -- naming
  21.     public static long[] addDigitInMass(long[] mass, int numDig) {
  22.         if (mass.length == numDig + 1) {
  23.             // :NOTE: same -- naming
  24.             long[] mass1 = new long[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.         long[][] mass = new long[1][1];
  34.         // :NOTE: same -- naming
  35.         long[] massSize = new long[1];
  36.         long[] numbersInColumns = new long[1];
  37.         long[] sumColumn = new long[1];
  38.         long[] sumString = new long[1];
  39.         int numStr = 0;
  40.         int numElem = 0;
  41.         while (in.hasNextLine()) {
  42.             Scanner str = new Scanner(in.nextLine());
  43.             while (str.hasNextInt()) {
  44.                 mass[numStr][numElem] = str.nextInt();
  45.                 sumString[numStr] +=mass[numStr][numElem];
  46.                 sumColumn[numElem] += mass[numStr][numElem];
  47.                 numbersInColumns[numElem]++;
  48.                 sumColumn = addDigitInMass(sumColumn, numElem);
  49.                 numbersInColumns = addDigitInMass(numbersInColumns, numElem);
  50.                 mass[numStr] = addDigitInMass(mass[numStr], numElem);
  51.                 numElem++;
  52.             }
  53.             mass = addLineInMass(mass, numStr);
  54.             massSize = addDigitInMass(massSize, numStr);
  55.             sumString = addDigitInMass(sumString, numStr);
  56.             massSize[numStr] = numElem;
  57.             numStr++;
  58.             numElem = 0;
  59.         }
  60.         // :NOTE: your scanner is not used in the next "for" block of code, so you should close it here
  61.         for (int i = 0; i < numStr; i++) {
  62.             for (int j = 0; j < massSize[i]; j++) {
  63.                 System.out.print((sumString[i] + sumColumn[j] - mass[i][j])/(numbersInColumns[j] + massSize[i] - 1) + " ");
  64.             }
  65.             System.out.println();
  66.         }
  67.         in.close();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement