From 4be68698dd31434d00ebc881d8c19a881ce2767d Mon Sep 17 00:00:00 2001 From: me Date: Fri, 30 Jan 2026 23:58:25 +0500 Subject: [PATCH] add solution for hw3:4142 & tests --- .gitea/workflows/reverse.yml | 2 +- java/reverse/ReverseAvg.java | 67 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 java/reverse/ReverseAvg.java diff --git a/.gitea/workflows/reverse.yml b/.gitea/workflows/reverse.yml index c685273..00988ab 100644 --- a/.gitea/workflows/reverse.yml +++ b/.gitea/workflows/reverse.yml @@ -19,4 +19,4 @@ jobs: - name: Run Reverse tests run: | - java -ea -cp out reverse.ReverseTest Base 3233 3435 3637 3839 + java -ea -cp out reverse.ReverseTest Base 3233 3435 3637 3839 4142 diff --git a/java/reverse/ReverseAvg.java b/java/reverse/ReverseAvg.java new file mode 100644 index 0000000..41f2097 --- /dev/null +++ b/java/reverse/ReverseAvg.java @@ -0,0 +1,67 @@ +package reverse; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Arrays; + +public class ReverseAvg { + public static void main(String[] args) throws IOException { + FastScanner sc = new FastScanner(); + int[][] lines = new int[8][]; + int linesCount = 0; + + while (sc.hasNextLine()) { + int[] line = new int[8]; + int count = 0; + + while (sc.hasNextInt()) { + if (count >= line.length) { + line = Arrays.copyOf(line, line.length * 2); + } + line[count++] = sc.nextInt(); + } + sc.nextLine(); + + line = Arrays.copyOf(line, count); + + if (linesCount >= lines.length) { + lines = Arrays.copyOf(lines, lines.length * 2); + } + lines[linesCount++] = line; + } + + printResult(linesCount, lines); + } + + private static void printResult(int linesCount, int[][] lines) { + PrintWriter out = new PrintWriter(System.out); + + for (int i = 0; i < linesCount; i++) { + int[] line = lines[i]; + + for (int j = 0; j < line.length; j++) { + if (j > 0) out.print(" "); + + long sum = 0; + long count = 0; + + for (int m = 0; m < lines[i].length; m++) { + sum += lines[i][m]; + count++; + } + + for (int k = 0; k < linesCount; k++) { + if (k != i && lines[k].length > j) { + sum += lines[k][j]; + count++; + } + } + + out.print(sum / count); + } + out.println(); + } + + out.flush(); + } +}