Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.*;
  3.  
  4. public class ReverseSum {
  5. public static void main(String[] args) {
  6. FastScanner input = null;
  7. try {
  8. input = new FastScanner(System.in);
  9. List<List<Integer>> matrix = new ArrayList<>();
  10. List<Integer> sumInRow = new ArrayList<>();
  11. List<Integer> sumInCol = new ArrayList<>();
  12. while (!input.endOfStream()) {
  13. int sumInCurRow = 0;
  14. List<Integer> curRow = new ArrayList<>();
  15. try {
  16. while (!input.endOfLine()) {
  17. int nextInt = input.nextIntInLine();
  18. if (curRow.size() > sumInCol.size() - 1) {
  19. sumInCol.add(nextInt);
  20. } else {
  21. sumInCol.set(curRow.size(), sumInCol.get(curRow.size()) + nextInt);
  22. }
  23. curRow.add(nextInt);
  24. sumInCurRow += nextInt;
  25. }
  26. } catch (NumberFormatException e) {
  27. //If this exception was caught,
  28. } // it means that there are no numbers left to read in current line
  29. matrix.add(curRow);
  30. sumInRow.add(sumInCurRow);
  31. }
  32. for (int i = 0; i < matrix.size(); i++) {
  33. for (int j = 0; j < matrix.get(i).size(); j++) {
  34. System.out.print(sumInCol.get(j) + sumInRow.get(i) - matrix.get(i).get(j) + " ");
  35. }
  36. System.out.println();
  37. }
  38. } catch (IOException e) {
  39. //System.out.println("I/O exceptions");
  40. } finally {
  41. if (input != null) {
  42. try {
  43. input.close();
  44. } catch (IOException e) {
  45. //System.out.println("I/O exceptions");
  46. }
  47. }
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement