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: Wrong Answer

Solution code:
while True:
	n=int(input())
	if not n: break
	a=[list(map(int,input().split())) for _ in range(n)]
	t=[[0 for _ in range(n+1)] for _ in range(n+1)]
	for i in range(n):
		for j in range(n):
			t[i][j]=a[i][j]
			t[i][n]+=a[i][j]
			t[n][j]+=a[i][j]
			t[n][n]+=a[i][j]
	print(a)
	print(t)

Submission status: Wrong Answer

Solution code:
while True:
	n=int(input())
	if not n: break
	a=[list(map(int,input().split())) for _ in range(n)]
	t=[[0 for _ in range(n+1)] for _ in range(n+1)]
	for i in range(n):
		for j in range(n):
			t[i][j]=a[i][j]
			t[i][n]+=a[i][j]
			t[n][j]+=a[i][j]
			t[n][n]+=a[i][j]
	print(t)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    else: # n > 0
        table = []
        for m in range(n):
            line = []
            
            line = map(int,raw_input().split())

            line_sum = sum(line)
            line.append(line_sum)
            table.append(line)
            
        
        
        vert_sum_line=[]
        for k in range(n):
            vert_sum = 0
            for j in range(n):
                vert_sum += table[j][k]
            vert_sum_line.append(vert_sum)
        
        total = sum(vert_sum_line)
        vert_sum_line.append(total)
    
        # .rjust(5)
        for line in table:
            print [str(x).rjust(5) for x in line]
        print [str(x).rjust(5) for x in vert_sum_line]

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
while n != 0:
    input_list = []
    for x in range(n):
        input_list.append(map(int, raw_input().split(' ')))
    for x in range(n):
        input_list[x].append(sum(input_list[x]))
        print ''.join(map(lambda n:"{:>5}".format(n), input_list))

    input_len = len(input_list[0])
    column_list = []
    for x in range(input_len):
        tmp = 0
        for y in range(n):
            tmp += input_list[y][x]
        column_list.append(tmp)
    print ''.join(map(lambda n:"{:>5}".format(n), column_list))
    n = int(raw_input())

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    while True:
        flag = input()
        if flag == 0:
            break
        num, ans = [], [0]*(flag + 1)
        for i in xrange(flag):
            num.append(map(int, raw_input().split()))
        for i in xrange(len(num)):
            for j in xrange(len(num[i])):
                print ("     " + str(num[i][j]))[-4:],
                ans[j] += num[i][j]
            print ("     " + str(sum(num[i])))[-4:]
            ans[j+1] += sum(num[i])

        for i in xrange(flag + 1):
            if i == flag:
                print ("     " + str(ans[i]))[-4:]
            else:
                print ("     " + str(ans[i]))[-4:],

Submission status: Wrong Answer

Solution code:
from sys import stdin

if __name__ == '__main__':
    for n in stdin:
        if int(n) == 0:
            break

        bottom_total = []
        right_total = []
        for _ in range(int(n)):
            line = input().split(' ')
            total = 0
            for i, v in enumerate(line):
                print(repr(int(v)).rjust(5), end='')
                total += int(v)
                if len(bottom_total) < _ + 1:
                    bottom_total.append(int(v))
                else:
                    bottom_total[_] += int(v)
            right_total.append(total)
            print(repr(right_total[_]).rjust(5))

        for num in bottom_total:
            print(repr(num).rjust(5), end='')
        print(repr(sum(right_total)).rjust(5))

Submission status: Wrong Answer

Solution code:
from sys import stdin

if __name__ == '__main__':
    is_begin = False
    for n in stdin:
        if int(n) == 0:
            break
        else:
            if is_begin:
                print()
        is_begin = True

        bottom_total = []
        right_total = []
        for _ in range(int(n)):
            line = input().split(' ')
            total = 0
            for i, v in enumerate(line):
                print(repr(int(v)).rjust(5), end='')
                total += int(v)
                if len(bottom_total) < _ + 1:
                    bottom_total.append(int(v))
                else:
                    bottom_total[_] += int(v)
            right_total.append(total)
            print(repr(right_total[_]).rjust(5))

        for num in bottom_total:
            print(repr(num).rjust(5), end='')
        print(repr(sum(right_total)).rjust(5), end='')

Submission status: Wrong Answer

Solution code:
n=int(input())
def sum1(x,y):
    num=0
    for i in range(n):
        num+=x[i][y]
    return int(num)
            
Matrix=[]
for i in range(n+1):
    Matrix.append(i)
    if i==n:
        Matrix[i]=[]
        for j in range(5):
            Matrix[i].append(sum1(Matrix,j))
    else:
        Matrix[i]=[int(k) for k in input().split()]
        Matrix[i].append(sum(Matrix[i]))

for i in range(n+1):
    for j in range(5):
        print(Matrix[i][j],end=" ")
    print("")

Submission status: Wrong Answer

Solution code:
def rowSum(matrix):
  n=len(matrix)
  sumcolumn=[0]*n
  for i in range(0,n):
    sumcolumn[i] = sum(matrix[i])
  return sumcolumn

def colSum(matrix):
  n=len(matrix)
  sumrow=[0]*n
  for i in range(0,n):
    for j in range(0,n):
      sumrow[i]+=matrix[j][i]
  return sumrow

n=int(input())
matrix=[None]*n
for i in range(0,n):
  matrix[i] = list(map(int,input().split()))

c=rowSum(matrix)
r=colSum(matrix)

matrix.append(r)

for i in range(0,n):
  matrix[i].append(c[i])

matrix[n].append(sum(matrix[n]))

for row in matrix:
  line=""
  for element in row:
    line += '{:>5}'.format(str(element))
  line.rstrip()
  print(line)

Submission status: Wrong Answer

Solution code:
def main():
    while True:
        n = int(input())
        if n == 0:
            break

        for i in range(n):
            num = 0
            m = map(int, input().split())
            for j in m:
                print("{:5}".format(j), end='')
                num += j
            print("{:5}".format(num))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
while True:
    inputCount = int(input())

    if inputCount == 0:
        break

    table = []

    for lp in range(inputCount):
        content = [int(item) for item in input().split(" ")]
        content.append(sum(content))
        table.append(content)

    table.append([])

    for col in range(inputCount + 1):
        total = 0
        for row in range(inputCount):
            total += table[row][col]

        table[inputCount].append(total)

    output = []
    for array in table:
        content = ["{:>5}".format(str(item)) for item in array]
        output.append(str(content))

    print("\n".join(output))

Submission status: Wrong Answer

Solution code:
while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for _ in[0]*n:
  b=list(map(int,input().split()))
  a+=[b+[sum(b)]]
  for s in b:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    while True:
        n = input()
        if n > 0:
            mat = [[0 for i in xrange(n +1)] for j in xrange(n + 1)]
        #input
        for i in range(n):
            line = sys.stdin.readline().split()
            for j in range(n):
                mat[i][j] = int(line[j])        
        #end input
        #core
        for i in xrange(n):
            mat[i][n] = sum(mat[i][j] for j in range(n))
            mat[n][i] = sum(mat[j][i] for j in range(n))
        mat[n][n] = sum(mat[i][n] for i in range(n))
        #core
        for lst in mat:
            print lst
        else:
            sys.exit()

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
while 1:
  n=input()+1
  if n==1: break
  x=[]
  y=[0]*n
  N=range(n)
  for i in N[:-1]:
    a=map(int,raw_input().split(' '))
    a.append(sum(a))
    x.append(a)
    for j in N: y[j]+=a[j]
  x.append(y)
  for i in N: print "".join([str(e).rjust(5) for e in x[i]])

Submission status: Accepted

Solution code:
while 1:
  n=input()+1
  if n==1: break
  x=[]
  y=[0]*n
  for _ in range(n-1):
    a=map(int,raw_input().split(' '))
    a.append(sum(a))
    s=""
    for i in range(n):
      y[i]+=a[i]
      s+=str(a[i]).rjust(5)
    print s
  print "".join([str(e).rjust(5) for e in y])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
while True:
	n=int(input())
	if not n: break
	a=[list(map(int,input().split())) for _ in range(n)]
	t=[[0 for _ in range(n+1)] for _ in range(n+1)]
	for i in range(n):
		for j in range(n):
			t[i][j]=a[i][j]
			t[i][n]+=a[i][j]
			t[n][j]+=a[i][j]
			t[n][n]+=a[i][j]
	for i in range(n+1):
		print("".join(map("{0:>5}".format,t[i])))

Submission status: Accepted

Solution code:
#!/usr/bin/python3

while True:
    n = int(input())
    if (n == 0):
        break
    mat = [ [] for _ in range(n) ]
    row = [ 0 for _ in range(n) ]
    col = [ 0 for _ in range(n) ]
    for i in range(n):
        ln = list(map(int, input().split(' ')))
        mat[i] = ln
        row[i] = sum(ln)
        for j in range(n):
            col[j] += ln[j]

    total = sum(col)

    for i in range(n):
        ls = mat[i]
        ls.append(row[i])
        print(''.join(['%5d' % x for x in ls]))

    col.append(sum(col))
    print(''.join(['%5d' % x for x in col]))

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

def main():
    while(True):
        # 入力
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        print "".join(map(lambda x_: str(x_).rjust(5), x))
        # for y in x:
        #     print("{0:4d}".format(y)),
        # else:
        #     print

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

def main():
    while(True):
        # 入力
        N = int(raw_input())
        if N == 0:
            break

        inp = []
        for x in range(N):
            input_list = map(int, raw_input().split())
            inp.append(input_list)

        printAns(inp)

def printAns(num_list):
    add_list = []
    for x in num_list:
        x.append(sum(x))
        add_list.append(x)

    total_list = [sum(x) for x in zip(*add_list)]
    add_list.append(total_list)
    print5digit(add_list)

def print5digit(num_list):
    for x in num_list:
        print "".join(map(lambda x_: str(x_).rjust(5), x))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break
    else: # n > 0
        table = []
        for m in range(n):
            line = []
            
            line = map(int,raw_input().split())

            line_sum = sum(line)
            line.append(line_sum)
            table.append(line)
            
        
        
        vert_sum_line=[]
        for k in range(n):
            vert_sum = 0
            for j in range(n):
                vert_sum += table[j][k]
            vert_sum_line.append(vert_sum)
        
        total = sum(vert_sum_line)
        vert_sum_line.append(total)
    
        # .rjust(5)
        temp = []
        for line in table:
            temp.append( [str(x).rjust(5) for x in line])
        temp.append([str(x).rjust(5) for x in vert_sum_line])
        
        for i in xrange(n+1):
            print "".join(temp[i])

Submission status: Accepted

Solution code:
while True:
    data=[]
    n = int(input())
    if n==0:
        break
    for i in range(n):
        spam=list(map(int,input().split()))
        spam.append(sum(spam))
        data.append(spam)
    spam=[]
    for i in range(n+1):
        spam.append(sum(x[i] for x in data))
    data.append(spam)
    for i in range(n+1):
        for j in range(n+1):
            print('{:5}'.format(data[i][j]),end='')
        print('')

Submission status: Accepted

Solution code:
while True:
	n = int(raw_input())
	if n == 0:
		break
	a = [map(int, raw_input().split()) for i in xrange(n)]
	a = [l + [sum(l)] for l in a]
	a.append([sum([l[i] for l in a]) for i in xrange(len(a[0]))])
	print "\n".join(["".join([str(i).rjust(5) for i in l]) for l in a])

Submission status: Accepted

Solution code:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        F=[]
        for i in range(n):
            F.append([int(x) for x in raw_input().split()])
        F.append([0 for x in range(n+1)])
        for i in range(n):
            F[i]=F[i]+[sum(F[i])]
        for j in range(n+1):
            s=0
            for i in range(n):
                s=s+F[i][j]
            F[n][j]=s
        G=[[' '*(5-len(str(x)))+str(x) for x in y] for y in F]
        for i in range(n+1):
            print ''.join(G[i])

Submission status: Accepted

Solution code:
while True:
    num = input()
    if num == 0: break

    mrx = []
    row = [0] * (num + 1)
    for i in range(num):
        mrx.append(map(int, raw_input().split()))
        mrx[i].append(sum(mrx[i]))
        for j in range(num + 1): row[j] += mrx[i][j]
    mrx.append(row)
    for i in mrx: print "".join([str(s).rjust(5) for s in i])

Submission status: Accepted

Solution code:
while True:
    data=[]
    n=int(input())
    if n==0:break
    for i in range(n):
        a=list(map(int,input().split(' ')))
        a.append(sum(a))
        data.append(a)
    a=[]
    for i in range(n+1):
        a.append(sum(x[i] for x in data))
    data.append(a)
    for i in range(n+1):
        for j in range(n+1):
            print('{:5}'.format(data[i][j]),end='')
        print('')

Submission status: Accepted

Solution code:
while True:
    N = int(raw_input())
    if N == 0: break
    total = None
    result = []
    for i in range(N):
        lis = list(map(int, raw_input().split(' ')))
        lis.append(sum(lis))
        result.append(lis)
        if total == None:
            total = lis[:]
        else:
            for i in range(len(total)):
                total[i] = total[i]+lis[i]
    else:
        result.append(total)
        for line in result:
            ans = []
            for v in line:
                ans.append('%05s' % v)
            else:
                print ''.join(ans)

Submission status: Accepted

Solution code:
def print_row(iterable):
    print(''.join(map(lambda x: str(x).rjust(5), iterable)),
          str(sum(iterable)).rjust(5), sep='')


while True:
    n = int(input())
    if n == 0:
        break

    m = []
    for i in range(n):
        m.append(list(map(int, input().split())))

    last = [0 for i in range(n)]
    for row in m:
        print_row(row)
        for (i, e) in enumerate(row):
            last[i] += e

    print_row(last)

Submission status: Accepted

Solution code:
import sys
try:
	while 1:
		temp = input()
		if (temp==0):
			break
		N = temp
		a = []
		for i in range(N):
			a.append(map(int, raw_input().split())+[0])
		a.append([0 for i in range(0,temp+1)])
		for i in range(0,temp):
			for j in range(0,temp):
				a[i][temp] += a[i][j]
		for k in range(0,temp+1):
			for l in range(0,temp):
				a[temp][k] += a[l][k]
		for m in range(0,temp+1):
			for n in range(0,temp+1):
				sys.stdout.write(str(a[m][n]).rjust(5))
			print ""
except(RuntimeError):
	pass

Submission status: Accepted

Solution code:
while True:
  l = []
  n = int(input())
  if n == 0:
    break
  lx = [0 for i in range(n+1)]
  for j in range(n):
    l1 = [int(i) for i in input().split()]
    c = 0
    for i in l1:
      c += i
    l1.append(c)
    for i in range(n+1):
      lx[i] += l1[i]
    l.append(l1)
  l.append(lx)
  for s in l:
    a = ''
    for t in s:
      a += ' ' * (5-len(str(t)))
      a += str(t)
    print(a)

Submission status: Accepted

Solution code:
n = int(raw_input())
while n != 0:
    input_list = []
    for x in range(n):
        input_list.append(map(int, raw_input().split(' ')))
    for x in range(n):
        input_list[x].append(sum(input_list[x]))
        print ''.join(map(lambda n:"{:>5}".format(n), input_list[x]))

    input_len = len(input_list[0])
    column_list = []
    for x in range(input_len):
        tmp = 0
        for y in range(n):
            tmp += input_list[y][x]
        column_list.append(tmp)
    print ''.join(map(lambda n:"{:>5}".format(n), column_list))
    n = int(raw_input())

Submission status: Accepted

Solution code:
from sys import stdin

if __name__ == '__main__':
    # is_begin = False
    for n in stdin:
        if int(n) == 0:
            break
        # else:
        #     if is_begin:
        #         print()
        # is_begin = True

        bottom_total = []
        right_total = []
        for _ in range(int(n)):
            line = input().split(' ')
            total = 0
            for i, v in enumerate(line):
                print(repr(int(v)).rjust(5), end='')
                total += int(v)
                if len(bottom_total) < i + 1:
                    bottom_total.append(int(v))
                else:
                    bottom_total[i] += int(v)
            right_total.append(total)
            print(repr(right_total[_]).rjust(5))

        for num in bottom_total:
            print(repr(num).rjust(5), end='')
        print(repr(sum(bottom_total)).rjust(5))

Submission status: Accepted

Solution code:
def output_res(row):
    print(''.join(map(lambda num: str(num).rjust(5), row)), str(sum(row)).rjust(5), sep='')


def main():
    while True:
        n = int(input())
        if n == 0:
            break

        bottom_total = [0 for _ in range(n)]
        for _ in range(n):
            each_line = list(map(int, input().split(' ')))
            output_res(each_line)
            bottom_total = [x + y for x, y in zip(bottom_total, each_line)]

        output_res(bottom_total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def print_list_inline(tr_list):
    inline = map(lambda string:"{:>5}".format(string),tr_list)
    inline = list(inline)
    print(("").join(inline))


while True:
    n = int(input())
    if n != 0:
        sum_col = [0 for i in range(n+1)]
        for i in range(n):
            list_row = list(map(int,input().split(" ")))
            sum_row = sum(list_row)
            list_row.append(sum_row)
            for j in range(len(list_row)):
                sum_col[j] += list_row[j]
            print_list_inline(list_row)
        print_list_inline(sum_col)
    else:
        break

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    a = []
    b = []
    for i in range(n):
        a.append([int(x) for x in input().split()])
    for i in range(n):
        a[i].append(sum(a[i]))
    for i in range(n + 1):
        sum_ = 0
        for j in range(n):
            sum_ += a[j][i]
        b.append(sum_)
    a.append(b)

    for i in range(n + 1):
        for j in range(n + 1):
            if j == n:
                print('{:>5}'.format(a[i][j]))
            else:
                print('{:>5}'.format(a[i][j]), end='')

Submission status: Accepted

Solution code:
while 1:
    n=int(raw_input())
    if n==0:break
    a=[map(int,raw_input().split()) for _ in xrange(n)]
    ans=[[0]*(n+1) for _ in xrange(n+1)]
    for i in xrange(n+1):
        for j in xrange(n+1):
            if i==n :
                if j==n:
                    ans[n][n]+=sum(ans[n])
                else:
                    for k in xrange(n):
                        ans[n][j]+=a[k][j]
            else:
                if j==n:
                    for k in xrange(n):
                        ans[i][n]+=a[i][k]
                else:
                    ans[i][j]=a[i][j]
    for i in xrange(n+1):
        print("".join(map(lambda x:str(x).rjust(5),ans[i])))

Submission status: Accepted

Solution code:
while True:
	n = int(input())
	if n == 0:
		break
	a = [list(map(int, input().split())) for i in range(n)]
	t = [[0 for i in range(n + 1)] for i in range(n + 1)]
	for i in range(n):
		for j in range(n):
			t[i][j] = a[i][j]
			t[i][n] += a[i][j]
			t[n][j] += a[i][j]
			t[n][n] += a[i][j]
	for i in range(n + 1):
		print("".join(map("{0:>5}".format,t[i])))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    row = n
    tab = []
    for i in range(row):
        r = list(map(int, input().split()))
        r.append(sum(r))
        tab.append(r)

    bel = []
    for i in range(row):
        tmp = 0
        for j in range(row):
            tmp += tab[j][i]
        bel.append(tmp)
    bel.append(sum(bel))
    tab.append(bel)

    for i in range(row+1):
        for j in range(row+1):
            #print("".join(map("{0:>5}".format, str(tab[i][j]))))
            print('{:>5}'.format(str(tab[i][j])), end='')
        print()

Submission status: Accepted

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

    for j in range(n):
        sum_num = 0
        for k in range(n):
            sum_num += Num_lis[j][k]
        Num_lis[j].append(sum_num)

    Num_lis.append([])
    for l in range(n + 1):
        wa = 0
        for m in range(n):
            wa += Num_lis[m][l]
        Num_lis[n].append(wa)
    for o in range(n + 1):
        s_num = ""
        for p in range(n + 1):
            s_num = s_num + '{:>5}'.format(Num_lis[o][p])
        print(s_num)

Submission status: Accepted

Solution code:
def rowSum(matrix):
  n=len(matrix)
  sumcolumn=[0]*n
  for i in range(0,n):
    sumcolumn[i] = sum(matrix[i])
  return sumcolumn

def colSum(matrix):
  n=len(matrix)
  sumrow=[0]*n
  for i in range(0,n):
    for j in range(0,n):
      sumrow[i]+=matrix[j][i]
  return sumrow

while True:
  n=int(input())
  if n==0:  break
  matrix=[None]*n
  for i in range(0,n):
    matrix[i] = list(map(int,input().split()))

  c=rowSum(matrix)
  r=colSum(matrix)

  matrix.append(r)

  for i in range(0,n):
    matrix[i].append(c[i])

  matrix[n].append(sum(matrix[n]))

  for row in matrix:
    line=""
    for element in row:
      line += '{:>5}'.format(str(element))
    line.rstrip()
    print(line)

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    a = []
    for i in range(n):
        a+=[list(map(int, input().split()))]
        a[i]+=[sum(a[i])]
        for j in range(n):print('%5d'%a[i][j],end='')
        print('%5d'%a[i][n])
    a=list(zip(*a[::-1]))
    for i in range(n):print('%5d'%sum(a[i]),end='')
    print('%5d'%sum(a[n]))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    arr = [[0] * n] * n
    for i in range(n):
        arr[i] = list(map(int,input().split()))

    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += arr[i][j]
        arr[i].append(tmp)

    last_line = []
    for j in range(n):
        tmp = 0
        for i in range(n):
            tmp += arr[i][j]
        last_line.append(tmp)

    tmp = 0
    for i in range(n):
        tmp += last_line[i]
    last_line.append(tmp)

    for i in range(len(arr[i])-1):
        for j in range(len(arr[i])):
            print('{0:5d}'.format(arr[i][j]),end = '')
        print('')
    for i in range(len(last_line)):
        print('{0:5d}'.format(last_line[i]),end = '')
    print('')

Submission status: Accepted

Solution code:
from copy import deepcopy

def main():
    while True:
        n = int(input())
        if n == 0:
            break

        tate = [0 for i in range(n)]

        for i in range(n):
            num = 0
            m = map(int, input().split())
            hoge = deepcopy(m)

            tate = [x + y for (x, y) in zip(tate, hoge)]

            for j in m:
                print("{:5}".format(j), end='')
                num += j
            print("{:5}".format(num))

        num = 0
        for j in tate:
            print("{:5}".format(j), end='')
            num += j
        print("{:5}".format(num))

if __name__ == '__main__':
    main()

Submission status: Accepted

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('{0:5d}'.format, l))) 
    print(''.join(map('{0:5d}'.format, bottom_record)))
    n = int(input())

Submission status: Accepted

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

    for i in range(0, n):
        l = list(map(int, input().split()))
        l.append(sum(l))
        bottom_record = [x + y for(x,y) in zip(bottom_record, l)]
        print(''.join(map('{0:5d}'.format, l))) 
    print(''.join(map('{0:5d}'.format, bottom_record)))
    n = int(input())

Submission status: Accepted

Solution code:
def main():
    while True:
        N = int(input())
        if N == 0:
            break

        A = [
            list(map(int, input().split(" ")))
            for _ in range(N)
        ]
        tot = [sum(col[r] for col in A) for r in range(N)]
        A.append(tot)
        for col in A:
            col.append(sum(col))

        for col in A:
            print("".join("{:5}".format(x) for x in col))


if __name__ == "__main__":
    main()

Submission status: Accepted

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))
        for j in range(n + 1):
            print("%5d" % (a[j]), end = "")
            total[j] += a[j]
        print()
    for i in range(n + 1):
        print("%5d" % (total[i]), end = "")
    print()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os

for s in sys.stdin:
    n = int(s)
    if n == 0:
        break

    M = []
    for i in range(n):
        lst = list(map(int, input().split()))
        lst.append(sum(lst))
        M.append(lst)

    last_lst = []
    # vertical sum
    for i in range(n+1):
        sumv = 0
        for row in M:
            sumv += row[i]
        last_lst.append(sumv)
    M.append(last_lst)

    # answer
    for row in M:
        for val in row:
            print(str(val).rjust(5), end='')
        print()

Submission status: Accepted

Solution code:
def form(x, e=""):
    print("{:>5}".format(x), end=e)

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:
            form(r)
        form(sum(row), "\n")
    columns = list(zip(*rows))
    for column in columns:
        form(sum(column))
    form(sum(sum(row) for row in rows), "\n")

Submission status: Accepted

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:
        print("".join(map("{0:>5}".format, row)), end="")
        print("{:>5}".format(sum(row)))
    columns = list(zip(*rows))
    print("".join(map("{0:>5}".format, list(sum(column) for column in columns))), end="")
    print("{0:>5}".format(sum(sum(row) for row in rows)))

Submission status: Accepted

Solution code:
while(1):
	box = []
	num = int(input())
	if num == 0: break
	[box.append(list(map(int,input().split()))) for i in range(num)]
	[box[i].append(sum(box[i])) for i in range(num)]
	box_replace = list(map(list, zip(*box)))
	temp = [[]]
	[temp[0].append(sum(box_replace[i])) for i in range(num+1)]
	box += temp
	box = [list(map(str, box[i])) for i in range(num+1)]
	box = ([[ box[i][j].rjust(5," ") for j in range(num+1)] for i in range(num+1)] )
	
	[print("".join(box[i])) for i in range(num+1)]

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0102&lang=jp
"""
import sys


def solve(data):
    for d in data:
        d.append(sum(d))

    v_sum = [0] * len(data[0])
    for d in data:
        for x, n in enumerate(d):
            v_sum[x] += n
    data.append(v_sum)
    return data


def main(args):
    # data = []
    # data.append([52, 96, 15, 20])
    # data.append([86, 22, 35, 45])
    # data.append([45, 78, 54, 36])
    # data.append([16, 86, 74, 55])
    while True:
        num = int(input())
        if num == 0:
            break
        data = []
        for _ in range(num):
            data.append([int(x) for x in input().split(' ')])
        result = solve(data)
        for row in result:
            for n in row:
                print('{:5d}'.format(n), end='')
            print('')


if __name__ == '__main__':
    main(sys.argv[1:])