PHP 程序检查矩阵是否为稀疏矩阵


2022年3月9日, Learn eTutorial
1636

什么是稀疏矩阵?

当矩阵中 **0** 的数量多于其他数字时,该矩阵被称为稀疏矩阵。例如,

 0  1  0  

 6  0  0

 7  0  0

在上面的例子中,我们可以看到有 **6** 个 **0**,只有 **3** 个非 **0**,所以我们可以说它是一个稀疏矩阵。

如何使用 PHP 检查矩阵是否为稀疏矩阵?

在此程序中,我们将从用户那里接收矩阵以检查矩阵是否稀疏。首先,我们必须将行数和列数读入变量 **row** 和 **col**,然后使用 for 循环将矩阵的值读入数组 **a[]**,根据 **row** 和 **col**。然后我们将值 0 赋给变量 **countZero** 以计算矩阵中的 **0** 的数量,并将 **'col * row'** 的计算值赋给变量 **matrix**。之后,我们将值 **0** 赋给变量 **i** 并执行循环,直到条件 **'i < row'** 变为 false,并在每次迭代中增加变量 **i** 的值。在循环块中,我们必须执行另一个循环,其中我们将值 **0** 赋给变量 **j** 并执行循环,直到条件 **'j < col'** 变为 false,并在每次迭代中增加变量 **j** 的值。在循环块中,我们必须检查条件 **'a[i][j] == 0'**,如果为真,则增加变量 **countZero** 的值。循环完成后,检查条件 **'countZero > (matrix / 2)'**,如果为真,则将当前矩阵打印为稀疏矩阵,否则打印为非稀疏矩阵。

算法

步骤 1: 初始化一个空数组 **a[]**

步骤 2: 从用户那里接收变量 **row** 和 **col** 的值,以存储行数和列数

步骤 3: 使用 for 循环将值接收到数组 **a[]** 中

步骤 4: 使用 for 循环打印数组 **a[]** 中的元素

步骤 5: 创建一个变量 **countZero**,其值为 **0**,用于计算矩阵中的 **0** 的数量

步骤 6: 将 **'col * row'** 的计算值赋给变量 matrix

步骤 7: 将值 **0** 赋给变量 **i** 并执行子步骤,直到条件 **'i < row'** 变为 false,并在每次迭代中增加变量 **i** 的值

        (i) 将值 **0** 赋给变量 **j** 并执行子步骤,直到条件 **'j < col'** 变为 false,并在每次迭代中增加变量 **j** 的值

            (a) 检查条件 **'a[i][j] == 0'**,如果为真,则增加变量 **countZero** 的值

步骤 8: 检查条件 **'countZero > (matrix / 2)'**,如果为真,则将当前矩阵打印为稀疏矩阵,否则打印为非稀疏矩阵

PHP 源代码

                                          <?php
$a = array();
$row = readline("Enter the number of rows: \n");
$col = readline("Enter the number of columns: \n");
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        $a[$i][$j] = readline("Enter the value at position $i $j :  ");
    }
}
echo "The entered matrix is matrix: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $a[$i][$j] . " ";
    }
    echo "\n";
}
$countZero = 0;
$matrix = $col * $row;
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        if ($a[$i][$j] == 0) {
            $countZero++;
        }
    }
}
if ($countZero > ($matrix / 2)) {
    echo "The current matrix is a sparse matrix";
} else {
    echo "The current matrix is not a sparse matrix";
}
?>
                                      

输出

Example 1
Enter the number of rows:  3
Enter the number of columns:  3
Enter the value at position 0 0 :  0
Enter the value at position 0 1 :  0
Enter the value at position 0 2 :  0
Enter the value at position 1 0 :  0
Enter the value at position 1 1 :  8
Enter the value at position 1 2 :  5
Enter the value at position 2 0 :  6
Enter the value at position 2 1 :  0
Enter the value at position 2 2 :  8
The entered matrix is matrix:
0 0 0
0 8 5
6 0 8
The current matrix is a sparse matrix

Example 2
Enter the number of rows:  3
Enter the number of columns:  3
Enter the value at position 0 0 :  1
Enter the value at position 0 1 :  0
Enter the value at position 0 2 :  2
Enter the value at position 1 0 :  0
Enter the value at position 1 1 :  5
Enter the value at position 1 2 :  4
Enter the value at position 2 0 :  8
Enter the value at position 2 1 :  0
Enter the value at position 2 2 :  0
The entered matrix is matrix:
1 0 2
0 5 4
8 0 0
The current matrix is not a sparse matrix