【问题标题】:in Java I have to check if a given matrix is a latin square or not在Java中我必须检查给定的矩阵是否是拉丁方
【发布时间】:2016-04-13 20:14:52
【问题描述】:

我的老师给了我们一个矩阵,我们应该编写一个代码来检查它是否是拉丁方格。我有所有的部件,但我无法将它们整理好,以便它们正常工作。 这是她让我们阅读她创建的矩阵的内容。

文本文件是matrix.txt,下面是它的内容。

3

1 2 3

3 1 2

2 3 1

正如你所看到的,这是一个拉丁方阵,不过,她说我们可以更改矩阵以确保它适用于她给我们提出的其他问题。

     import java.io.File;
     import java.io.FileNotFoundException;
     import java.util.Scanner;
     public class Multidim {

public static void main(String args[]){
    int matrix[][] = initMatrix();
    //printData(matrix); //Uncomment to print array

    /////YOUR MAIN CODE HERE/////



}

///PLACE YOUR METHODS HERE

public static int[][] initMatrix(){
    int matrix[][];
    Scanner filein = null;
    try {
        filein = new Scanner(new File("matrix.txt"));
        int numRows = Integer.parseInt(filein.nextLine());
        matrix = new int[numRows][];
        parseData(matrix, filein);
        filein.close();
        return matrix;
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
        if(filein != null)
            filein.close();
        return null;
    }
}

public static void parseData(int matrix[][], Scanner in){
    for(int r = 0; r < matrix.length; r++){
        String splitLine[] = in.nextLine().split(" ");
        matrix[r] = new int[splitLine.length];
        for(int c = 0; c < matrix[r].length; c++){
            matrix[r][c] = Integer.parseInt(splitLine[c]);
        }
    }
}

public static void printData(int matrix[][]){
    for(int r = 0; r < matrix.length; r++){
        for(int c = 0; c < matrix[r].length; c++){
            System.out.print(matrix[r][c] + " ");
        }
        System.out.println();
    }
}

}

这是我目前拥有的代码。去哪儿了?

    public static boolean LatinSquare(int[][]array) {
    for(int i=0;i<array.length;i++) {
        for(int j=0; j<array[i].length; j++)  {
           if(i!=j)   {
               return false;
           }
        }
    }
    return true;
}
public boolean DuplicatesInRows(int[][]array) {
    for (int i=0; i<array.length; i++)  {
        for (int j=0;j<array[i].length; j++) {
            int num=array[i][j];
            for(int col =j+1; col<array.length; col++) {
                if(num==array[i][col])  {
                    return true;
                }
            }
        }
    }
    return false;
   }
public boolean DuplicatesInCol(int[][]array) {
    for(int i=0;i<array.length; i++)  {
        for(int j=0; j<array.length; j++)  {
            for(int k=1; k<array.length; k++)  {
                if (array[i][j+k]==array[i][j])  {
                    if (array[i][j]!=0)  {
                        return true;
                    }
                }
            }
        }
    }
    return false;
    }

我也不知道这是怎么回事……

  if(LatinSquare(matrix)==false)
  System.out.println("This is not a Latin Square");
  else
  System.out.println("This is a Latin Square");

【问题讨论】:

  • 您需要提供“matrix.txt”文件或输入文件格式
  • 如何给你 matrix.txt 文件?如果您想创建自己的 matrix.txt 文件,它只是一个 3x3 矩阵,是一个拉丁正方形。
  • 将其添加到问题中,并使用适当的换行符
  • 好的,我将它添加到问题中,但不确定我是否正确地做。请告诉我。
  • 如果满足您的问题,请点击投票箭头下方的绿色复选框接受我的回答。

标签: java matrix latin-square


【解决方案1】:

我会这样做

Multidim.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Multidim 
{

    public static void main(String args[])
    {
        int matrix[][] = initMatrix();
        // printData(matrix); //Uncomment to print array
        if(latinSquare(matrix))
        {
            System.out.println("This is a Latin Square");
        }
        else
        {
            System.out.println("This is not a Latin Square");          
        } 
    }

    public static boolean latinSquare(int[][] array) 
    {
        for (int i = 0; i<array.length ;i++) 
        {
            // check for duplicates in each row
            if(duplicates(array[i]))
            {
                return false;
            }

            // create a column array
            int[] column = new int[array[i].length]; 
            for(int j = 0; j<array.length; j++)
            {
                column[j] = array[j][i]; // could throw an exception if the input file isn't square 3x3, 2x2, 4x4, etc
            }

            // check for duplicates in each column
            if(duplicates(column))
            {
                return false;
            }
        }
        return true;
    }

    public static boolean duplicates(int[] array)
    {
        for (int i = 0; i<array.length; i++) 
        {
            for(int j = 0; j<array.length; j++)
            {
                if (i != j && array[i] == array[j])
                {
                    return true;
                }
            }    
        }
        return false;
    }

    ///PLACE YOUR METHODS HERE
    public static int[][] initMatrix()
    {
        int matrix[][];
        Scanner filein = null;
        try 
        {
            filein = new Scanner(new File("matrix.txt"));
            int numRows = Integer.parseInt(filein.nextLine());
            matrix = new int[numRows][];
            parseData(matrix, filein);
            filein.close();
            return matrix;
        } 
        catch (FileNotFoundException e) 
        {
            System.out.println(e.getMessage());
            if(filein != null)
            {
                filein.close();
            }
            return null;
        }
    }

    public static void parseData(int matrix[][], Scanner in)
    {
        for(int r = 0; r < matrix.length; r++)
        {
            String splitLine[] = in.nextLine().split(" ");
            matrix[r] = new int[splitLine.length];
            for(int c = 0; c < matrix[r].length; c++)
            {
                matrix[r][c] = Integer.parseInt(splitLine[c]);
            }
        }
    }

    public static void printData(int matrix[][])
    {
        for(int r = 0; r < matrix.length; r++)
        {
            for(int c = 0; c < matrix[r].length; c++)
            {
                System.out.print(matrix[r][c] + " ");
            }
            System.out.println();
        }
    }
}

ma​​trix.txt

3
1 2 3
3 1 2
2 3 1

注意你的方法应该遵循Oracle's code conventions

方法应该是动词,大小写混合,首字母小写,每个内部单词的首字母大写。

这就是我将LatinSquare 更改为latinSquare 的原因

【讨论】:

  • 感谢您的帮助。但是,当我尝试使用另一个矩阵来测试它并确保它是正确的时,它不起作用。无论我使用什么矩阵,它都说它是一个拉丁方阵,即使它不是。这是因为在所有布尔值完成之前具有 system.out.println 的第一部分吗?
  • 你能把不适合你的测试用例给我吗?我已经用非拉丁方格对其进行了测试。将第一行从1 2 3 更改为1 2 1 导致This is not a Latin Square。此外,Java 是异步的,除非您生成一个新线程。布尔值总是首先返回。
  • 我可能打错了,我去看看。我使用的矩阵是 3x3 {123}{321}{231}。我会再试一次
  • 好吧,我作弊并做了一个复制粘贴,以确保我没有输错任何东西并且它有效。那谢谢啦。现在我希望能够让程序确定问题出在非拉丁方格中的位置。比如它在第 2 列第 2 行。这可能吗?
  • @LisaS 我会克制自己不要做你的整个家庭作业。那不是您发布的原始问题。我会给你一个线索,是的,有可能,看看使用的索引变量(ij)。这些正在计算正在检查的数组的当前位置。
猜你喜欢
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多