Printing string in rows and column pattern Java

前端 未结 7 2261
孤独总比滥情好
孤独总比滥情好 2021-02-07 14:31

i\'m just created a java project to print string that is given in rows and column just like matrix. Here\'s the output that i just made:

h e l l o 
_ w o r l 
d _         


        
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-07 15:22

    you could try to make the spiral algorithm first and try to find the value of its each index in the matrix so that later you could map every index of your string into the specific index in the spiral array matrix.

    for example:

    Input: n = 5
    Output:   1   2   3   4   5
              16  17  18  19  6
              15  24  25  20  7
              14  23  22  21  8
              13  12  11  10  9
    Aligned Output:  1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
    

    the algorithm can be found here or here.

    now you know all the index of each position to make the letters aligned in a spiral way, what you have to do is map each letter of your string to be print according to the number of the spiral matrix sequentially.

    print string 1.
    print string 2.
    print string 3.   
    print string 4.
    print string 5.
    print string 16.
    print string 17.
    print string 18.
    print string 19.
    print string 6.
    print string 15.
    cont...
    

提交回复
热议问题