提问者:小点点

按行和列打印字符串模式Java


我刚刚创建了一个Java项目,打印字符串,给出的行和列,就像矩阵。 下面是我刚才的输出:

h e l l o 
_ w o r l 
d _ i t s 
_ b e a u 
t i f u l 

有没有可能像这样把输出呈现成螺旋状?

h e l l o
_ b e a _
s u l u w
t f i t o
i _ d l r

要澄清这个螺旋矩阵是如何产生的:

下面是我当前的代码:

String str = "hello world its beautiful";
    double length = Math.sqrt(str.length());
    int x = (int) length;

    for (int i = 0, len = str.length(); i < len; i++) {
        System.out.print(str.charAt(i) + " ");
        if (i % x == x - 1) {
            System.out.println();
        }
    }

我正试着做同样的模式,但永远都做不到。 让我知道你能帮我这件事。 我很感激你给出的每一个答案,谢谢。


共3个答案

匿名用户

您可以先尝试使用螺旋算法,并尝试在矩阵中找到它的每个索引的值,以便稍后可以将字符串的每个索引映射到螺旋数组矩阵中的特定索引中。

例如:

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

算法可以在这里找到,也可以在这里找到。

现在你知道了每个位置的所有索引,使字母以螺旋方式对齐,你要做的就是按照螺旋矩阵的编号顺序映射你要打印的字符串的每个字母。

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...

匿名用户

SpiralMatrix(int s)返回s x s螺旋矩阵。

static int[][] spiralMatrix(int s) {
    int[][] a = new int[s][s];
    int n = 0;
    for (int b = s - 1, c = 0, x = 0, y = 0, dx = 0, dy = 1; b > 0; b -= 2, x = y = ++c)
        for (int j = 0, t = 0; j < 4; ++j, t = dx, dx = dy, dy = -t)
            for (int i = 0; i < b; ++i, x += dx, y += dy, ++n)
                a[x][y] = n;
    if (s % 2 == 1)
        a[s / 2][s / 2] = n;
    return a;
}

测试

for (int s = 0; s < 6; ++s) {
    int[][] a = spiralMatrix(s);
    System.out.println("s=" + s);
    for (int[] row : a)
        System.out.println(Arrays.toString(row));
    System.out.println();
}

结果

s=0

s=1
[0]

s=2
[0, 1]
[3, 2]

s=3
[0, 1, 2]
[7, 8, 3]
[6, 5, 4]

s=4
[0, 1, 2, 3]
[11, 12, 13, 4]
[10, 15, 14, 5]
[9, 8, 7, 6]

s=5
[0, 1, 2, 3, 4]
[15, 16, 17, 18, 5]
[14, 23, 24, 19, 6]
[13, 22, 21, 20, 7]
[12, 11, 10, 9, 8]

你可以用这个方法来做。

String str = "hello world its beautiful";
int[][] spiral = spiralMatrix(5);
int length = str.length();
for (int x = 0, h = spiral.length, w = spiral[0].length; x < h; ++x) {
    for (int y = 0; y < w; ++y) {
        int p = spiral[x][y];
        System.out.print((p < length ? str.charAt(p) : " ") + " " );
    }
    System.out.println();
}

结果

h e l l o 
  b e a   
s u l u w 
t f i t o 
i   d l r 

匿名用户

基本上,您在字符串中从头到尾移动,但将stringbuffer视为数组。 你还需要跟踪你的方向(dx,dy)和你的界限在哪里。

以下代码将生成:

hello
beau 
 l.tw
sufio
i dlr

给定输入“Hello world is Beautiful”

public class Main {

    public static void main(String[] args) {
        String text ="hello world is beautiful";
        int len = text.length();
        double sideLength = Math.sqrt( len );
        int width = 0;
        int height = 0;

        // check if it's a square
        if ( sideLength > (int) sideLength) {
            // nope... it#s a rectangle
            width = (int) sideLength +1;
            height = (int) Math.ceil((double)len / (double)width);
        } else {
            // square
            width = (int) sideLength;
            height = (int) sideLength;
        }

        // create a buffer for the spiral
        StringBuffer buf = new StringBuffer( width * height );
        buf.setLength( width * height );
        // clear it.
        for (int a=0; a < buf.length(); a++ ) {
            buf.setCharAt(a, '.');
        }
        

        int dstX = 0;
        int dstY = 0;
        int curWidth =  width;
        int curHeight = height;
        int startX = 0;
        int startY = 0;
        int dx = 1;
        int dy = 0;
        // go through the string, char by char
        for (int srcPos =0; srcPos < len; srcPos++) {
            buf.setCharAt( dstX + dstY * width, text.charAt( srcPos ));

            // move cursor
            dstX += dx;
            dstY += dy;

            // check for bounds
            if ( dstX == curWidth-1 && dx > 0) {
                // end of line while going right, need to go down
                dx = 0;
                dy = 1;
                // also, reduce width
                curWidth--;
                startY++;
            } else if (dstY == curHeight-1 && dy > 0) {
                // end of column while going down, need to go left
                dx = -1;
                dy = 0;

                // also, reduce height
                curHeight--;
            } else if (dstX == startX && dx < 0) {
                // hit left border while going left, need to go up
                dx = 0;
                dy = -1;
                // also, increase startX
                startX++;
            } else if (dstY == startY && dy < 0) {
                // hit top border, while going up, need to go right
                dx = 1;
                dy = 0;
                // also, increase startY
                startY++;
            }
            
        }


        // display string
        for (int line = 0; line < height; line++) {
            System.out.println( buf.substring( line* width, line*width +width) );
        }
    }
}