Home AtCoder. 004 Cross Sum(2)
Post
Cancel

AtCoder. 004 Cross Sum(2)

[Link] https://AtCoder.jp/contests/typical90/tasks/typical90_d


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
import java.io.*;

public class Main {
  public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());
    StringBuilder sb = new StringBuilder();
    int h = Integer.parseInt(st.nextToken());
    int w = Integer.parseInt(st.nextToken());
    int[][] arr = new int[h][w];
    int[] colSum = new int[w];
    int[] rowSum = new int[h];
    for(int i = 0; i < h; i++) {
      st = new StringTokenizer(br.readLine());
      for(int j = 0; j < w; j++) {
        arr[i][j] = Integer.parseInt(st.nextToken());
      }
    }
    for(int i = 0; i < h; i++)
      for(int j = 0; j < w; j++) rowSum[i] += arr[i][j];
    for(int i = 0; i < w; i++)
      for(int j = 0; j < h; j++) colSum[i] += arr[j][i];

    for(int i = 0; i < h; i++) {
      for(int  j = 0; j < w; j++) sb.append(rowSum[i] + colSum[j] - arr[i][j] + " ");
      sb.append("\n");
    }
    System.out.println(sb);
  }
}
This post is licensed under CC BY 4.0 by the author.