Loop diagonally through two dimensional array

前端 未结 12 1346
刺人心
刺人心 2020-12-05 11:52

I wrote the following code to walk half the diagonals of an array:

String[][] b = [a,b,c]
               [d,e,f]
               [g,h,i];  

public void LoopD         


        
12条回答
  •  无人及你
    2020-12-05 11:52

    Type 1: We can solve this using python very easily

    Code:

    r,c=map(int,input().split())
    mat=[[str(ch) for ch in input().split()]for _ in range(r)]
    for i in range(r*c-2):
        lis=[]
        for j in range(r):
            for k in range(c):
                if k+j==i:
                    lis.append(mat[j][k])
        print(*lis[::-1])
    

    Output:

    5 5
    a b c d e
    f g h i j
    k l m n o
    p q r s t
    u v w x y
    
    a
    f b
    k g c
    p l h d
    u q m i e
    v r n j
    w s o
    x t
    y
    

提交回复
热议问题