<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 以對角線圖案打印矩陣 > 原文: [https://www.geeksforgeeks.org/print-matrix-diagonal-pattern/](https://www.geeksforgeeks.org/print-matrix-diagonal-pattern/) 給定 n * n 大小的矩陣,任務是以對角線圖案打印其元素。 ![](https://img.kancloud.cn/b6/f8/b6f82fb1922976a725190e2f3001c27c_661x384.png) ``` Input : mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : 1 2 4 7 5 3 6 8 9. Explanation: Start from 1 Then from upward to downward diagonally i.e. 2 and 4 Then from downward to upward diagonally i.e 7, 5, 3 Then from up to down diagonally i.e 6, 8 Then down to up i.e. end at 9. Input : mat[4][4] = {{1, 2, 3, 10}, {4, 5, 6, 11}, {7, 8, 9, 12}, {13, 14, 15, 16}} Output: 1 2 4 7 5 3 10 6 8 13 14 9 11 12 15 16 . Explanation: Start from 1 Then from upward to downward diagonally i.e. 2 and 4 Then from downward to upward diagonally i.e 7, 5, 3 Then from upward to downward diagonally i.e. 10 6 8 13 Then from downward to upward diagonally i.e 14 9 11 Then from upward to downward diagonally i.e. 12 15 then end at 16 ``` **方法**:從圖中可以看出,每個元素要么斜向上打印,要么斜向下打印。 從索引(0,0)開始,對角向上打印元素,然后更改方向,改變列,對角向下打印。 該循環一直持續到到達最后一個元素為止。 **算法**: 1. 創建變量 *i = 0,j = 0* 以存儲行和列的當前索引 2. 運行從 0 到 n * n 的循環,其中 n 是矩陣的邊。 3. 使用標志 **isUp** 來確定方向是向上還是向下。 最初將 *isUp = true* 設置為向上方向。 4. 如果 isUp = 1,則通過增加列索引和減少行索引來開始打印元素。 5. 同樣,如果 isUp = 0,則減少列索引并增加行索引。 6. 移至下一個列或行(下一個開始的行和列) 7. 這樣做直到遍歷所有元素。 **實現**: ## C++ ```cpp // C++ program to print matrix in diagonal order #include <bits/stdc++.h> using namespace std; const int MAX = 100; void printMatrixDiagonal(int mat[MAX][MAX], int n) { ????// Initialize indexes of element to be printed next ????int i = 0, j = 0; ????// Direction is initially from down to up ????bool isUp = true; ????// Traverse the matrix till all elements get traversed ????for (int k = 0; k < n * n;) { ????????// If isUp = true then traverse from downward ????????// to upward ????????if (isUp) { ????????????for (; i >= 0 && j < n; j++, i--) { ????????????????cout << mat[i][j] << " "; ????????????????k++; ????????????} ????????????// Set i and j according to direction ????????????if (i < 0 && j <= n - 1) ????????????????i = 0; ????????????if (j == n) ????????????????i = i + 2, j--; ????????} ????????// If isUp = 0 then traverse up to down ????????else { ????????????for (; j >= 0 && i < n; i++, j--) { ????????????????cout << mat[i][j] << " "; ????????????????k++; ????????????} ????????????// Set i and j according to direction ????????????if (j < 0 && i <= n - 1) ????????????????j = 0; ????????????if (i == n) ????????????????j = j + 2, i--; ????????} ????????// Revert the isUp to change the direction ????????isUp = !isUp; ????} } int main() { ????int mat[MAX][MAX] = { { 1, 2, 3 }, ??????????????????????????{ 4, 5, 6 }, ??????????????????????????{ 7, 8, 9 } }; ????int n = 3; ????printMatrixDiagonal(mat, n); ????return 0; } ```
                  <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>

                              哎呀哎呀视频在线观看