Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确定矩阵的对称性[][]_Java_Matrix_Symmetry - Fatal编程技术网

Java 确定矩阵的对称性[][]

Java 确定矩阵的对称性[][],java,matrix,symmetry,Java,Matrix,Symmetry,我试图找出矩阵[][]在不同方向(horitzonzal/垂直或两者)是否对称,并找到了本教程 用我的矩阵试过了,但似乎不能正常工作。 以下是我当前的代码和得到的输出: 示例矩阵: 111 144 144 我通过以下方式调用教程中的方法: // test for symmetrie checkHV(matrix, rows, colums); 其中,本例中的行和列均为3。 依我看,它应该不输出,但当前输出是垂直的(总是,所有矩阵都是…)。为什么呢?我怎样才能修改代码,使其正确地为

我试图找出矩阵[][]在不同方向(horitzonzal/垂直或两者)是否对称,并找到了本教程 用我的矩阵试过了,但似乎不能正常工作。 以下是我当前的代码和得到的输出:

示例矩阵:

111
144
144
我通过以下方式调用教程中的方法:

 // test for symmetrie 
    checkHV(matrix, rows, colums);
其中,本例中的行和列均为3。 依我看,它应该不输出,但当前输出是垂直的(总是,所有矩阵都是…)。为什么呢?我怎样才能修改代码,使其正确地为我工作?谢谢

下面是教程中的代码:

 static void checkHV(int [][]arr, int N,
                int M)
{

// Initializing as both horizontal 
// and vertical symmetric.
boolean horizontal = true;
boolean vertical = true;

// Checking for Horizontal Symmetry. 
// We compare first row with last
// row, second row with second
// last row and so on.
for (int i = 0, k = N - 1; 
         i < N / 2; i++, k--)
{

    // Checking each cell of a column.
    for (int j = 0; j < M; j++)
    {

        // check if every cell is identical
        if (arr[i][j] != arr[k][j])
        {
            horizontal = false;
            break;
        }
    }
}

// Checking for Vertical Symmetry. We compare
// first column with last column, second xolumn
// with second last column and so on.
for (int i = 0, k = M - 1;
         i < M / 2; i++, k--)
{

    // Checking each cell of a row.
    for (int j = 0; j < N; j++)
    {
        // check if every cell is identical
        if (arr[i][j] != arr[k][j])
        {
            horizontal = false;
            break;
        }
    }
}

if (!horizontal && !vertical)
    System.out.println("NO");

else if (horizontal && !vertical)
    System.out.println("HORIZONTAL");

else if (vertical && !horizontal)
    System.out.println("VERTICAL");

else
    System.out.println("BOTH");
}
行=2,列=4

试试这个:

 static void checkHV(int [][]arr, int N,
            int M)
{

// Initializing as both horizontal 
// and vertical symmetric.
boolean horizontal = true;
boolean vertical = true;

// Checking for Horizontal Symmetry. 
// We compare first row with last
// row, second row with second
// last row and so on.
for (int i = 0, k = N - 1; 
         i < N / 2; i++, k--)
{

    // Checking each cell of a column.
    for (int j = 0; j < M; j++)
    {

        // check if every cell is identical
        if (arr[i][j] != arr[k][j])
        {
            horizontal = false;
            break;
        }
    }
}

// Checking for Vertical Symmetry. We compare
// first column with last column, second xolumn
// with second last column and so on.
for (int i = 0, k = M - 1;
         i < M / 2; i++, k--)
{

    // Checking each cell of a row.
    for (int j = 0; j < N; j++)
    {
        // check if every cell is identical
        if (arr[j][i] != arr[j][k])
        {
            vertical = false;
            break;
        }
    }
}

if (!horizontal && !vertical)
    System.out.println("NO");

else if (horizontal && !vertical)
    System.out.println("HORIZONTAL");

else if (vertical && !horizontal)
    System.out.println("VERTICAL");

else
    System.out.println("BOTH");
}
静态无效校验HV(int[]arr,int N,
int(M)
{
//初始化为两个水平
//垂直对称。
布尔水平=真;
布尔垂直=真;
//检查水平对称性。
//我们比较第一排和最后一排
//第二排,第二排
//最后一排等等。
对于(inti=0,k=N-1;
i
公共静态布尔值(int[][]矩阵){
对于(int-rowL=0,rowH=matrix.length-1;rowL
嗯。。。您将
vertical
设置为
true
,并且在任何情况下都不会将其设置为任何其他值应为
vertical=false“请写信给我们contribute@geeksforgeeks.org报告上述内容的任何问题。”您应该这样做。所有4种语言的代码中都存在相同的问题。非常好,谢谢!我认为代码是正确的。@lydiaP您还应该阅读有关使用调试器的内容。您可以在这里使用调试器快速识别问题。另外,永远不要相信你在互联网上找到的任何东西都是有效的。甚至像Oracle文档这样的“权威”来源也会有打字错误和遗漏。
 static void checkHV(int [][]arr, int N,
            int M)
{

// Initializing as both horizontal 
// and vertical symmetric.
boolean horizontal = true;
boolean vertical = true;

// Checking for Horizontal Symmetry. 
// We compare first row with last
// row, second row with second
// last row and so on.
for (int i = 0, k = N - 1; 
         i < N / 2; i++, k--)
{

    // Checking each cell of a column.
    for (int j = 0; j < M; j++)
    {

        // check if every cell is identical
        if (arr[i][j] != arr[k][j])
        {
            horizontal = false;
            break;
        }
    }
}

// Checking for Vertical Symmetry. We compare
// first column with last column, second xolumn
// with second last column and so on.
for (int i = 0, k = M - 1;
         i < M / 2; i++, k--)
{

    // Checking each cell of a row.
    for (int j = 0; j < N; j++)
    {
        // check if every cell is identical
        if (arr[j][i] != arr[j][k])
        {
            vertical = false;
            break;
        }
    }
}

if (!horizontal && !vertical)
    System.out.println("NO");

else if (horizontal && !vertical)
    System.out.println("HORIZONTAL");

else if (vertical && !horizontal)
    System.out.println("VERTICAL");

else
    System.out.println("BOTH");
}
public static boolean isSymmetricHorizontally(int[][] matrix) {
    for(int rowL = 0, rowH = matrix.length - 1; rowL < rowH; rowL++, rowH--)
        for(int col = 0; col < matrix[0].length; col++)
            if(matrix[rowL][col] != matrix[rowH][col])
                return false;
    
    return true;
}

public static boolean isSymmetricVertically(int[][] matrix) {
    for(int colL = 0, colH = matrix[0].length - 1; colL < colH; colL++, colH--)
        for(int row = 0; row < matrix[0].length; row++)
            if(matrix[row][colL] != matrix[row][colL])
                return false;
    
    return true;
}