【问题标题】:Abundant vs Non-Abundant Matrices丰富与非丰富矩阵
【发布时间】:2016-04-04 19:09:08
【问题描述】:

有人告诉我编写一个程序,该程序将读取一个包含三个数组的文件,每个数组的大小为 5 x 6,并包含许多 0 和非零数字。然后我要创建一个行数未定义但有 3 列的数组,它指示非零数字的位置。第一列是行索引,第二列是列索引。第三列包含实际的非零数本身。

这是一个非常迂回的程序。但我相信我的主要问题是这个--

  1. 当两个非零数字在同一行时,我无法在新矩阵中为这两个数字打印出相同的行索引。我尝试将 row-index 设置为 a ,然后将 row-index 设置为单独的行计数器,但它仍然会搞砸并逐行递增。此刻,我的脑海里一片空白。

打印代码的唯一方法是将 rind 设置为 1 而不是 0。但这会导致整个程序关闭;我已将其更改为 1 以更清楚地说明我的问题

也许这对于学校作业的要求有点过分,我深表歉意。如果这篇文章以某种方式违反了规则,我会立即删除它。

感谢任何愿意看一眼这一点的人。

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

public class Prog465h {


    static Scanner inFile = null;

    public static void main(String[]args) {



        try {

            // create scanner to read file
            inFile = new Scanner(new File ("prog465h.dat"));

        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
            System.exit(0);
        }





        while(inFile.hasNext()) {

            int rows = inFile.nextInt();
            int columns = inFile.nextInt();

            int rind = 1;

            int cr = 0; // count rows
            int cc = 0; // count zeroes

            int[][] first = new int[rows][columns];


            for (int a = 0; a < first.length; a++) {

                    // catch the next

                for (int b = 0; b < first[a].length; b++) {

                    first[a][b] = inFile.nextInt();

                }

            }

            for (int a = 0; a < first.length; a++) {


                for (int b = 0; b < first[a].length; b++) {

                    System.out.print(first[a][b] + " ");
                    if (first[a][b] != 0) {

                        rind++;

                    }

                }

                System.out.println(" ");

            }


            System.out.println("COUNTS ARE BELOW:");

            int[][] mod = new int [rind][3];    // new array based on non-zeroes

            for (int a = 0; a < first.length; a++) {
                    cc = 0;

                for (int b = 0; b < first[a].length; b++) {

                        if (first[a][b] == 0) { // if there is a 0 increase number of columns counted

                            cc++;

                        } else {    // if not--

                            mod[cr][2] = first[a][b];   // then make this nonzero number the last column of x row of mod.
                                                        // x row depends on...?
                                                        // the number of counted rows?
                            mod[cr][0] = (a+1); // put the number of rows counted for this number
                            mod[cr][1] = (cc+1); // put the number of 0's (aka columns) counted for this number

                            cc = 0;

                        }




                }

                cr++;



            }

            for (int a = 0; a < mod.length; a++) {


                for (int b = 0; b < mod[a].length; b++) {

                    System.out.print(mod[a][b] + " ");


                }

                System.out.println(" ");

            }

            System.out.println("\n **** ALL DONE **** \n");

        }



    }

}

我的输出:(注意第一个矩阵如何打印三个 0。这不应该发生,它应该完全跳过该行。)

0 0 7 0 0 0  
0 0 0 0 -8 0  
0 0 0 0 0 0  
2 0 0 0 0 0  
0 0 0 0 0 0  
COUNTS ARE BELOW:
1 3 7  
2 5 -8  
0 0 0  
4 1 2  

 **** ALL DONE **** 

0 2 0 3 0 1  
8 0 4 0 1 0  
0 3 0 1 0 -7  
5 0 9 0 6 0  
0 2 0 -1 0 7  
COUNTS ARE BELOW:
1 2 1  
2 2 1  
3 2 -7  
4 2 6  
5 2 7  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  

 **** ALL DONE **** 

0 0 1 0 0 2  
3 0 0 4 0 0  
0 0 5 0 0 6  
7 0 0 8 0 0  
0 0 9 0 0 1  
COUNTS ARE BELOW:
1 3 2  
2 3 4  
3 3 6  
4 3 8  
5 3 1  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  
0 0 0  

 **** ALL DONE **** 

样本输出(输出应该是什么):

Original Matrix
   0   0   7   0   0   0
   0   0   0   0  -8   0
   0   0   0   0   0   0
   2   0   0   0   0   0
   0   0   0   0   0   0
    1    3    7
    2    5   -8
    4    1    2
The Original Matrix is Sparse


Original Matrix
   0   2   0   3   0   1
   8   0   4   0   1   0
   0   3   0   1   0  -7
   5   0   9   0   6   0
   0   2   0  -1   0   7
The Original Matrix is Abundant


Original Matrix
   0   0   1   0   0   2
   3   0   0   4   0   0
   0   0   5   0   0   6
   7   0   0   8   0   0
   0   0   9   0   0   1
    1    3    1
    1    6    2
    2    1    3
    2    4    4
    3    3    5
    0    0    9
    4    1    7
    4    4    8
    5    3    9
    5    6    1
The Original Matrix and the Sparse Matrix
are Equally Efficient

文件:

5
6
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
5
6
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
5
6
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1

【问题讨论】:

    标签: java arrays multidimensional-array logic


    【解决方案1】:

    感谢您坦诚地对待这项家庭作业。一般来说,如果您发布这样的问题,我认为您在获得帮助时不会有问题。您显然已经尝试过这个问题,并且非常接近于让它发挥作用。

    我看过了,看来您只需将cr++; 向上移动一点。将它向上移动几行,使其位于内部 for 循环内(因此它会在 cc = 0; 行之后立即执行另外,请确保初始化 int rind = 0;(而不是 1)。您还需要更改cc = 0;cc++;

    当我运行它时,它会生成您在问题中发布的示例输出。

    只是一个观察,但您也可以通过将 2 个 for 循环压缩为一个来稍微整理一下代码:

               for (int a = 0; a < first.length; a++) {
                    // catch the next
                    for (int b = 0; b < first[a].length; b++) {
                        first[a][b] = inFile.nextInt();
                    }
                }
    
                for (int a = 0; a < first.length; a++) {
                    for (int b = 0; b < first[a].length; b++) {
                        System.out.print(first[a][b] + " ");
                        if (first[a][b] != 0) {
                            rind++;
                        }
                    }
                    System.out.println(" ");
                }
    

    可能变成:

             for (int a = 0; a < first.length; a++) {
                    // catch the next
                    for (int b = 0; b < first[a].length; b++) {
                        first[a][b] = inFile.nextInt();
                        System.out.print(first[a][b] + " ");
                        if (first[a][b] != 0) {
                            rind++;
                        }
                    }
                    System.out.println(" ");
                }
    

    【讨论】:

    • 哇,非常感谢!它现在完美运行。我也会回去纠正我的 for 循环。感谢您到目前为止并给了我很好的建议。祝你有美好的一天!
    • @Nikolas 很高兴为您提供帮助。我确实注意到在最后一个矩阵上的输出是错误的。您需要在内部循环中将cc=0; 更改为cc++。我已经编辑了我的答案。
    猜你喜欢
    • 2011-07-27
    • 2018-08-31
    • 2023-04-08
    • 2011-08-29
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    相关资源
    最近更新 更多