Problem:


Matrix-like Computation

Your task is to develop a tiny little part of spreadsheet software.

Write a program which adds up columns and rows of given table as shown in the following figure:

Input

The input consists of several datasets. Each dataset consists of:

n (the size of row and column of the given table)
1st row of the table
2nd row of the table
 :
 :
nth row of the table

The input ends with a line consisting of a single 0.

Output

For each dataset, print the table with sums of rows and columns. Each item of the table should be aligned to the right with a margin for five digits. Please see the sample output for details.

Sample Input

4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
0

Output for the Sample Input

   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815
   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815



Sample input:
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
4
52 96 15 20
86 22 35 45
45 78 54 36
16 86 74 55
0


Sample output:
   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815
   52   96   15   20  183
   86   22   35   45  188
   45   78   54   36  213
   16   86   74   55  231
  199  282  178  156  815

Submission status: WA: Presentation Error

Solution code:
import functools

n = int(input())
while(n):
    bottom_record = [0]*(n+1)

    for i in range(0, n):
        l = list(map(int, input().split()))
        l.append(functools.reduce(lambda x, y: x + y, l))
        bottom_record = [x + y for(x,y) in zip(bottom_record, l)]
        print(' '.join(map(str, l))) 
    print(' '.join(map(str, bottom_record)))
    n = int(input())

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    total = [0] * (n + 1)
    for i in range(n):
        a = [int(i) for i in input().split()]
        a.append(sum(a))
        print(" ".join(map(str, a)))
        for j in range(n + 1):
            total[j] += a[j]
    print(" ".join(map(str, total)))

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    rows = [list(map(int, input().split())) for _ in range(n)]
    for row in rows:
        for r in row:
            print("{:5d} ".format(r), end="")
        print("{:5d}".format(sum(row)))
    columns = list(zip(*rows))
    for column in columns:
        print("{:5d} ".format(sum(column)), end="")
    print("{:5d}".format(sum(sum(row) for row in rows)))

Submission status: WA: Presentation Error

Solution code:
import sys
import math

while True:
	n = int(input())
	if n == 0:
		break
	data = [list(map(int,input().split())) for i in range(n)]
	data.append([0 for i in range(n+1)])

	for i in range(n+1):
		if i != n:
			data[i].append(sum(data[i]))
		for j in range(n):
			data[n][i] = data[n][i]+data[j][i]
	for i in range(n+1):
		for j in range(n+1):
			if j != n:
				string = str('{:>5}'.format(data[i][j]))
				sys.stdout.write(string)
				sys.stdout.write(' ')
			else:
				print('{:>5}'.format(data[i][j]))

Submission status: WA: Presentation Error

Solution code:
import sys
#from me.io import dup_file_stdin

def print2d(m):
    [print(" ".join(map(str,r))) for r in m]

#@dup_file_stdin
def solve():
    while True:
        n = int(sys.stdin.readline())
        if n == 0 :
            return
        m = []
        for i in range(n):
            m.append([int(x) for x in sys.stdin.readline().split(' ')])
        for r in range(n):
            m[r].append(sum(m[r]))
        lastRow = []
        for c in range(n+1):
            s = 0
            for r in range(n):
                s += m[r][c]
            lastRow.append(s)
        m.append(lastRow)
        print2d(m)
solve()

Submission status: WA: Presentation Error

Solution code:
while True:
  a=int(input())
  if a == 0: break
  b=[0]*a
  for i in range(a):
    d = map(str,raw_input().split())
    q=0
    for j in range(len(d)):
      b[j]+=int(d[j])
      q+=int(d[j])
      b.append(d)
      print d[j],
    print q
  w=0
  for j in range(a): w+=b[j]
  for i in range(a):
    print b[i],
  print w

Submission status: WA: Presentation Error

Solution code:
while True:
  a=int(input())
  tage=[0]*(a+1)
  if a ==0: break
  for i in range(a):
    su=0
    hoge=map(int,raw_input().split())
    for j in range(a):
      tage[j]+=hoge[j]
      su+=hoge[j]
      print "%5d"% (hoge[j]),
    else:
      tage[j+1]+=su
      print "%5d"%(su)
  for i in range(a):
    print "%5d"%(tage[i]),
  print "%5d"%(tage[i+1])

Submission status: WA: Presentation Error

Solution code:
while True:
  a = int(input())
  if a == 0: break
  tage,ans=0,[0]*a
  for i in range(a):
    hoge =map(int,raw_input().split())
    su=0
    for j in range(a):
      su+=hoge[j]
      ans[j]+=hoge[j]
      print "%4d"%(hoge[j]),
    else:
      print "%4d"%(su)
      tage+=su
  for i in range(a):
    print "%4d"%(ans[i]),
  else: print "%4d"%(tage)

Submission status: WA: Presentation Error

Solution code:
import sys
while True:
  a=int(input())
  if a==0: break
  ans2,tage=0,[0]*a
  for i in range(a):
    ans=0
    hoge=map(int,raw_input().split())
    sys.stdout.write(" ")
    for j in range(a):
      tage[j]+=hoge[j]
      print "%4d"%(hoge[j]),
      ans+=hoge[j]
    else: 
      ans2+=ans
      print "%4d"%(ans)
  else:
    sys.stdout.write(" ")
    for x in range(a):
      print "%4d"%(tage[x]),
    else: print "%4d"%(ans2)

Submission status: WA: Presentation Error

Solution code:
while True:
    n = int(raw_input())
    if n == 0: break
    
    a = []
    for i in range(n):
        a.append(map(int, raw_input().split()))
    
    for i in range(n):
        a[i].append(sum(a[i]))
    
    col_sum = []
    for col in range(len(a[0])):
        s = 0
        for row in range(len(a)):
            s += a[row][col];
        col_sum.append(s)
    a.append(col_sum)

    for row in range(len(a)):
        for col in range(len(a[0])):
            print "%5d" % a[row][col],
        print

Submission status: WA: Presentation Error

Solution code:
#coding:utf-8
ans=[]
while True:
    curAns=[]
    n=input()
    if n ==0:
        break
    for i in xrange(n):
        lst=map(int,raw_input().split())
        lst.append(sum(lst))
        curAns.append(lst)
    lLst=[]                             #最後のリスト
    for i in xrange(n+1):               #横方向探索
        verSum=0
        for j in xrange(n):             #縦方向探索
            verSum+=curAns[j][i]
        lLst.append(verSum)
    curAns.append(lLst)
    relAns=[]
    for i in curAns:
        tmpLst=map(str,i)
        nLst=[]
        for j in tmpLst:
            nLst.append(j.rjust(5))
        ansStr=' '.join(nLst)
        relAns.append(ansStr)
    ans.append(relAns)

for i in ans:
    for j in i:
        print j

Submission status: WA: Presentation Error

Solution code:
#coding:utf-8
ans=[]
while True:
    curAns=[]
    n=input()
    if n ==0:
        break
    for i in xrange(n):
        lst=map(int,raw_input().split())
        lst.append(sum(lst))
        curAns.append(lst)
    lLst=[]                             #最後のリスト
    for i in xrange(n+1):               #横方向探索
        verSum=0
        for j in xrange(n):             #縦方向探索
            verSum+=curAns[j][i]
        lLst.append(verSum)
    curAns.append(lLst)
    relAns=[]
    for i in curAns:
        tmpLst=map(str,i)
        nLst=[]
        for j in tmpLst:
            nLst.append(j.rjust(4))
        ansStr=' '.join(nLst)
        relAns.append(ansStr)
    ans.append(relAns)

for i in ans:
    for j in i:
        print j

Submission status: WA: Presentation Error

Solution code:
while True:
    n = input()
    ans = []
    if n == 0: break
    tmp = [0] * (n+1)
    for i in range(n):
        ans.append(map(int, raw_input().split()))
        ans[i].append(sum(ans[i]))
        for j in range(len(ans[i])):
            tmp[j] += ans[i][j]
    ans.append(tmp)
    for a in ans:
        print " ".join(map(str, a))

Submission status: WA: Presentation Error

Solution code:
while True:
    n = input()
    ans = []
    if n == 0: break
    tmp = [0] * (n+1)
    for i in range(n):
        ans.append(map(int, raw_input().split()))
        ans[i].append(sum(ans[i]))
        for j in range(len(ans[i])):
            tmp[j] += ans[i][j]
    ans.append(tmp)
    for a in ans:
        print " " + " ".join(map(str, a))

Submission status: WA: Presentation Error

Solution code:
while True:
    n = input()
    ans = []
    if n == 0: break
    tmp = [0] * (n+1)
    for i in range(n):
        ans.append(map(int, raw_input().split()))
        ans[i].append(sum(ans[i]))
        for j in range(len(ans[i])):
            tmp[j] += ans[i][j]
    ans.append(tmp)
    for a in ans:
        print " ".join(map(lambda x: str(x).rjust(5), a))