<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 檢查給定矩陣是否稀疏 > 原文: [https://www.geeksforgeeks.org/check-given-matrix-sparse-not/](https://www.geeksforgeeks.org/check-given-matrix-sparse-not/) 矩陣是具有 m 行和 n 列的二維數據對象,因此共有 m * n 個值。 如果矩陣的大多數值是 0,那么我們說矩陣是稀疏的。 考慮稀疏的定義,如果 0 的數量大于矩陣元素的一半,則認為矩陣是稀疏的, 例子: ``` Input : 1 0 3 0 0 4 6 0 0 Output : Yes There are 5 zeros. This count is more than half of matrix size. Input : 1 2 3 0 7 8 5 0 7 Output: No ``` 要檢查矩陣是否為稀疏矩陣,我們只需要檢查等于零的元素總數即可。 如果此計數大于(m * n)/ 2,則返回 true。 ## CPP ``` // CPP code to check if a matrix is // sparse. #include <iostream> using namespace std; const int MAX = 100; bool isSparse(int array[][MAX], int m, int n) { ????int counter = 0; ????// Count number of zeros in the matrix ????for (int i = 0; i < m; ++i) ????????for (int j = 0; j < n; ++j) ????????????if (array[i][j] == 0) ????????????????++counter; ????return (counter > ((m * n) / 2)); } // Driver Function int main() { ????int array[][MAX] = { { 1, 0, 3 },? ????????????????????????{ 0, 0, 4 },? ????????????????????????{ 6, 0, 0 } }; ????int m = 3, ????????n = 3; ????if (isSparse(array, m, n)) ????????cout << "Yes"; ????else ????????cout << "No"; } ``` ## Java ```java // Java code to check? // if a matrix is // sparse. import java.io.*; class GFG { ????static int MAX = 100; ????static boolean isSparse(int array[][], int m, int n) ????{ ????????int counter = 0; ????????// Count number of zeros in the matrix ????????for (int i = 0; i < m; ++i) ????????????for (int j = 0; j < n; ++j) ????????????????if (array[i][j] == 0) ????????????????????++counter; ????????return (counter > ((m * n) / 2)); ????} ????// Driver Function ????public static void main(String args[]) ????{ ????????int array[][] = { { 1, 0, 3 },? ????????????????????????????{ 0, 0, 4 },? ????????????????????????????{ 6, 0, 0 } }; ????????int m = 3, ????????????n = 3; ????????if (isSparse(array, m, n)) ????????????System.out.println("Yes"); ????????else ????????????System.out.println("No"); ????} } // This code is contributed by // Nikita Tiwari. ``` ## Python3 ```py # Python 3 code to check # if a matrix is # sparse. MAX = 100 def isSparse(array,m, n) : ????counter = 0 ????# Count number of zeros ????# in the matrix ????for i in range(0,m) : ????????for j in range(0,n) : ????????????if (array[i][j] == 0) : ????????????????counter = counter + 1 ????return (counter >? ????????????((m * n) // 2)) # Driver Function array = [ [ 1, 0, 3 ], ??????????[ 0, 0, 4 ], ??????????[ 6, 0, 0 ] ] m = 3 n = 3 if (isSparse(array, m, n)) : ????print("Yes") else : ????print("No") # this code is contributed by # Nikita tiwari ``` ## C# ```cs // C# code to check if a matrix is // sparse. using System; class GFG { ????static bool isSparse(int [,]array, int m, ???????????????????????????????????????int n) ????{ ????????int counter = 0; ????????// Count number of zeros in the matrix ????????for (int i = 0; i < m; ++i) ????????????for (int j = 0; j < n; ++j) ????????????????if (array[i,j] == 0) ????????????????????++counter; ????????return (counter > ((m * n) / 2)); ????} ????// Driver Function ????public static void Main() ????{ ????????int [,]array = { { 1, 0, 3 },? ?????????????????????????{ 0, 0, 4 },? ?????????????????????????{ 6, 0, 0 } }; ????????int m = 3, ????????????n = 3; ????????if (isSparse(array, m, n)) ????????????Console.WriteLine("Yes"); ????????else ????????????Console.WriteLine("No"); ????} } // This code is contributed by vt_m. ``` ## PHP ```php <?php // PHP code to check if a matrix is // sparse. $MAX = 100; function isSparse( $array, $m, $n) { ????$counter = 0; ????// Count number of zeros ????// in the matrix ????for ($i = 0; $i < $m; ++$i) ????????for ($j = 0; $j < $n; ++$j) ????????????if ($array[$i][$j] == 0) ????????????????++$counter; ????return ($counter > (($m * $n) / 2)); } ????// Driver Code ????$array = array(array(1, 0, 3),? ???????????????????array(0, 0, 4),? ???????????????????array(6, 0, 0)); ????$m = 3; ????$n = 3; ????if (isSparse($array, $m, $n)) ????????echo "Yes"; ????else ????????echo "No"; // This code is contributed by anuj_67\. ?> ``` Output: ``` Yes ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看