【问题标题】:java spiral matrix not workingjava螺旋矩阵不起作用
【发布时间】:2017-09-09 02:58:12
【问题描述】:

这是我编写的抛出螺旋矩阵的代码,但它抛出了 矩阵不同

2000021 
1970022 
1860123
1714131224 
1600025

代码是这样的:

public class caracol {
   public static void main(String[] args) {
        int[][] matriz = new int[5][5];
        int n=5;
        int nlimite= n-1;
        int inicio = 0;
        int c=1;
        while(c<=(n*n)) {

            for (int i = inicio+2; i<=nlimite-1; i++) //baja
            {
                matriz[i][nlimite-1] = c++; 
            }

            for (int i = inicio+2; i>=inicio+1; i--) ///izquierda
            {
                matriz[nlimite-1][i] = c++;
            }

            for (int i = nlimite-1; i>=inicio+1; i--) //sube
            {
                matriz[i][inicio+1] = c++;
            }


            for (int i = inicio+1; i>=nlimite-1; i++) //derecha
            {
                matriz[inicio+1][i] = c++;
            }

            for (int i = inicio+1; i<=nlimite; i++) //baja
            {
                matriz[i][nlimite] = c++;
            }

            for (int i = nlimite-1; i>=inicio; i--) ///izquierda
            {
                matriz[nlimite-1][i] = c++;
            }

            for (int i = nlimite; i>=inicio; i--) //sube
            {
                matriz[i][inicio] = c++;
            }

            for (int i = inicio; i>=nlimite; i++) //derecha
            {
                matriz[inicio][i] = c++;
            }  

            for (int i = inicio; i<=nlimite; i++) //baja
            {
                matriz[i][nlimite] = c++;
            }
            nlimite=nlimite-1;
            inicio = inicio+1;
        }
        for(int x=0;x<n;x++) { /*Mostrar la matriz en pantalla*/
            System.out.println();
            for(int y=0;y<n;y++) {
                System.out.print(matriz[x][y]);
            }
        }
    }
}

生成的矩阵与照片的矩阵不同。 您必须从矩阵 [2] [2] 开始,但在矩阵 [0] [0] 中也是如此,因为您更改了行和列的索引位置。

【问题讨论】:

  • 欢迎来到 SO。请编辑您的问题以显示生成的矩阵。将其添加到问题中,而不是在 cmets 中。
  • 如何编辑我的问题?
  • 点击下方的edit
  • i.stack.imgur.com/hmLVX.jpg 这是矩阵的结果。
  • 顺便说一句,生成这个有什么意义,什么时候它总是一样的?在初始化程序中给出值会更简单......

标签: java matrix spiral


【解决方案1】:

试试这样的:

public class Matrix
{
    // holds the matrix
    private int[][] matrix = new int[ 5 ][ 5 ];
    // fills up the matrix with zeroes   
    public void init()
    {
        for ( int i = 0; i < 5; ++i )
            Arrays.fill( matrix[i], 0 );
    }
    // prints the matrix out
    public void print()
    {
        for ( int i = 0; i < 5; ++i )
        {
            for ( int j = 0; j < 5; ++j )
                System.out.printf( "%3d", matrix[i][j] );
            System.out.println();
        }
    }
    // prints text out according to the matrix
    public void print(String text)
    {
        for ( int i = 0; i < 5; ++i )
        {
            for ( int j = 0; j < 5; ++j )
                System.out.print( text.charAt(matrix[i][j]) );
            System.out.println();
        }
    }
    // fills the matrix with the spiral
    public void generate()
    {
        // variables: co-ordinates, value, direction
        int x = 2, y = 2, val = 0, dir = 0;
        // in a 5×5 matrix we should put in 25 values...
        while ( val < 25 )
        {
            // put this value
            matrix[y][x] = ++val;
            // for debugging
            System.out.printf( "val=%d, x=%d, y=%d, dir=%d%n", val, x, y, dir );
            // calculate next value's position, and check if we must turn
            int turn = -1;
            switch ( dir )
            {
                case 0: // down, checking left
                    ++y;
                    if ( 25 != val ) // the last value would cause error
                        turn = matrix[y][x - 1];
                    break;
                case 1: // left, checking up
                    --x;
                    turn = matrix[y - 1][x];
                    break;
                case 2: // up, checking right
                    --y;
                    turn = matrix[y][x + 1];
                    break;
                case 3: // right, checking down
                    ++x;
                    turn = matrix[y + 1][x];
            }
            // next direction
            if ( 0 == turn )
                dir = ( dir + 1 ) & 3;
        }
    }
    // for testing
    public static void main( String[] args )
    {
        final Matrix m = new Matrix();
        m.init();
        m.generate();
        m.print();
        m.print("HelloWorldTodayIsAGoodDay");
    }
}

将输出:

val=1, x=2, y=2, dir=0
val=2, x=2, y=3, dir=1
val=3, x=1, y=3, dir=2
val=4, x=1, y=2, dir=2
val=5, x=1, y=1, dir=3
val=6, x=2, y=1, dir=3
val=7, x=3, y=1, dir=0
val=8, x=3, y=2, dir=0
val=9, x=3, y=3, dir=0
val=10, x=3, y=4, dir=1
val=11, x=2, y=4, dir=1
val=12, x=1, y=4, dir=1
val=13, x=0, y=4, dir=2
val=14, x=0, y=3, dir=2
val=15, x=0, y=2, dir=2
val=16, x=0, y=1, dir=2
val=17, x=0, y=0, dir=3
val=18, x=1, y=0, dir=3
val=19, x=2, y=0, dir=3
val=20, x=3, y=0, dir=3
val=21, x=4, y=0, dir=0
val=22, x=4, y=1, dir=0
val=23, x=4, y=2, dir=0
val=24, x=4, y=3, dir=0
val=25, x=4, y=4, dir=0
 17 18 19 20 21
 16  5  6  7 22
 15  4  1  8 23
 14  3  2  9 24
 13 12 11 10 25
sAGoo
IoWod
ylHrD
alela
doTdy

编辑:根据Me Myself 请求添加文本

【讨论】:

猜你喜欢
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多