0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

典型90問004Cross SumをPythonで解く!

Posted at

はじめに

AtCoder現在茶色(年内には緑が目標!)のあんこちゃん(@Chunky_RBP_chan)です。
典型問題は抑えとかねばということで、★2〜★5に取り組んでいます。
Pythonで取り組まれている方も多いと思うので自分の解答をシェアしたいと思います!

問題

解法

列に関する和と行に関する和を持つ配列をcolsum、rowsumとします。
i行j列で求める数はrowsum[i]+colsum[j]-A[i][j]であり、この計算をすべての要素について行えばよいです。

H,W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
colsum = [0]*W
rowsum = [0]*H
for i in range(H):
    rowsum[i] += sum(A[i])
    for j in range(W):
        colsum[j] += A[i][j]

ans = [[0]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        ans[i][j] = colsum[j]+rowsum[i]-A[i][j]
for i in range(H):
    print(' '.join(map(str,ans[i])))
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?