第五题代码

8 views
Skip to first unread message

韦锦鹏

unread,
Jan 2, 2019, 8:23:30 PM1/2/19
to cs10...@googlegroups.com

m,n = map(int,input().split())
matrix = [[int(x) for x in input().split()] for i in range(m)]

def main(matrix):
    rows = len(matrix)
    cols = len(matrix[0])
    new = [[0] * cols for i in range(rows)]
    for r in range(rows):
        for c in range(cols):
            count = 1
            summ = matrix[r][c]
            if r - 1 >= 0:
                count += 1
                summ += matrix[r-1][c]
                if c - 1 >= 0:
                    count += 1
                    summ += matrix[r-1][c-1]
                if c + 1 < cols:
                    count += 1
                    summ += matrix[r-1][c+1]
            if r + 1 < rows:
                count += 1
                summ += matrix[r+1][c]
                if c - 1 >= 0:
                    count += 1
                    summ += matrix[r+1][c-1]
                if c + 1 < cols:
                    count += 1
                    summ += matrix[r+1][c+1]
            if c - 1 >= 0:
                count += 1
                summ += matrix[r][c-1]
            if c + 1 < cols:
                count += 1
                summ += matrix[r][c+1]
            new[r][c] = summ//count
           
    for i in range(len(new)):
        new[i] = [str(j) for j in new[i]]
        s = " ".join(new[i])
        print(s)

main(matrix)

Reply all
Reply to author
Forward
0 new messages