0

Alright so I'm working on something where I take input from System.in; the first line is an int (n) representing the size of a matrix. The next n lines are the matrix itself like so:

10
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0
0 0 0 0 1 0 0 1 1 0
0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 1 1 0 0 0
1 1 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 

The problem is there may be multiple matrix's in a single input, so the next line would have another int and the corresponding matrix underneath until it hits a line with a single 0. I then have to pass each matrix along with the size at the top as a BufferedReader to a method which adds the numbers to a 2D array.

I'm just a little unsure on how to split the input up and send it to the method. Would making a new BufferedReader using skip() and specifying a size each time work? The biggest problem I seem to be running into is reading the size but then the size being excluded as it has already been read.

Cheers

EDIT: Got it working using Bhesh Gurung's method, thanks a ton. This is what I ended up with. I think some of the if statements are redundant but it works.

BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
ArrayList<GraphAdjMatrix> mat = new ArrayList<GraphAdjMatrix>();
try
    {
        String line = buffer.readLine().trim();
        String[] tokens = line.split("\\s+");
        boolean[][] adj;

        int n = Integer.parseInt(tokens[0]);

        while (n != 0) {
            if (tokens.length == 1 && n > 0) {

                adj = new boolean[n][n];

                for (int i = 0; i < n; i++) {
                    line = buffer.readLine().trim();
                    tokens = line.split("\\s+");
                    if (tokens.length != n)
                    {
                        throw new Error("bad format: adjacency matrix");
                    }

                    for (int j = 0; j < n; j++)
                    {
                        int entry = Integer.parseInt(tokens[j]);
                        adj[i][j] = entry != 0;
                    }
                }
                mat.add(new GraphAdjMatrix(adj, n));
            }
            line = buffer.readLine().trim();
            tokens = line.split("\\s+");
            n = Integer.parseInt(tokens[0]);
        }
    }
    catch (IOException x) { throw new Error("bad input stream"); }          
4

4 回答 4

1

BufferedReader.readLine使用该方法逐行读取输入。

对于每一行,使用String.split返回字符串数组的方法将其拆分。如果数组的大小为 1 并且唯一的元素是非零,则初始化一个二维数组,其大小为该数字。并用其余的行填充该数组并将该数组发送到该方法。当您找到另一个非零的单个整数时再次启动相同的过程,或者当它为 0 时退出。

您可以利用该Integer.parseInt方法从字符串中解析整数。

于 2012-10-15T03:14:27.173 回答
0

根据您的帖子,大小似乎是绝对任意的。即首先您将输入 2 x 3 矩阵,然后输入 3 x 1 矩阵。

在这种情况下,您需要读取维度。您的输入可以是这种格式

Enter the number of Matrices : 3

First Matrix
Rows :
Columns :
Elements : 

Second Matrix 
Rows : 
Columns :
Elements :

.
.

因此,您何时阅读!如果你在 google 和 codechef 中解谜。您将遇到类似的情况,您将提供测试用例的数量作为输入。

于 2012-10-15T03:13:08.737 回答
0

我相信你只需要使用一个while循环来处理传入数组的大小。

您甚至可以获取数组的大小并在 while 循环条件中使用整数。

        Scanner sc = new Scanner(System.in);
    int x;
    while((x = sc.nextInt()) != 0){
        for (int i = 0; i < x; i++){
            System.out.println("do this " + x + " times");
        }
    }

希望这可以帮助。

编辑:这可能不够清楚.. 在你的 while 循环中,你可以然后根据你的 x 变量填充一个二维数组(使用嵌套的 for 循环)。

因为输入的数量始终是已知的,所以这应该是管理数组创建输入的最简单方法。

于 2012-10-15T03:17:45.743 回答
0

假设用户输入正确,您是否试图实现这样的目标?

    Scanner sc= new Scanner(System.in);
    int[][] matrix = new int[1][1];
    int size = 0;

    String inputString = null;
    while(!"0".equals((inputString = sc.nextLine()))){
        String[] elements = inputString.split(" ");
        if(elements.length == 1){
            //this is size entry
            size = Integer.parseInt(elements[0]);
            matrix = new int[size][size];
        }else{
            for(int i=0; i< size; i++){
                inputString = sc.nextLine();
                elements = inputString.split(" ");
                for(int j=0; j<elements.length; j++){
                    matrix[i][j] = Integer.parseInt(elements[j]);
                }
            }
            //pass your size and matrix to other class/method
            // .....
        }
    }
于 2012-10-15T03:23:23.910 回答