Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.*;
- public class ReverseSum {
- public static void main(String[] args) {
- FastScanner input = null;
- try {
- input = new FastScanner(System.in);
- List<List<Integer>> matrix = new ArrayList<>();
- List<Integer> sumInRow = new ArrayList<>();
- List<Integer> sumInCol = new ArrayList<>();
- while (!input.endOfStream()) {
- int sumInCurRow = 0;
- List<Integer> curRow = new ArrayList<>();
- try {
- while (!input.endOfLine()) {
- int nextInt = input.nextIntInLine();
- if (curRow.size() > sumInCol.size() - 1) {
- sumInCol.add(nextInt);
- } else {
- sumInCol.set(curRow.size(), sumInCol.get(curRow.size()) + nextInt);
- }
- curRow.add(nextInt);
- sumInCurRow += nextInt;
- }
- } catch (NumberFormatException e) {
- //If this exception was caught,
- } // it means that there are no numbers left to read in current line
- matrix.add(curRow);
- sumInRow.add(sumInCurRow);
- }
- for (int i = 0; i < matrix.size(); i++) {
- for (int j = 0; j < matrix.get(i).size(); j++) {
- System.out.print(sumInCol.get(j) + sumInRow.get(i) - matrix.get(i).get(j) + " ");
- }
- System.out.println();
- }
- } catch (IOException e) {
- //System.out.println("I/O exceptions");
- } finally {
- if (input != null) {
- try {
- input.close();
- } catch (IOException e) {
- //System.out.println("I/O exceptions");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement