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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 按特定順序就地轉換矩陣 > 原文: [https://www.geeksforgeeks.org/in-place-convert-matrix-in-specific-order/](https://www.geeksforgeeks.org/in-place-convert-matrix-in-specific-order/) 編寫代碼以特定方式轉換矩陣,而無需使用額外的空間。 ``` Input: 1 2 3 4 5 6 7 8 9 Output: 1 6 7 2 5 8 3 4 9 ``` 乍一看,這個問題似乎與尋找矩陣的轉置類似。 但是,如果仔細看,您會發現輸出矩陣中的每個偶數列在輸入矩陣中都有對應行的元素以相反的順序排列。 **我們強烈建議您最小化瀏覽器,然后自己嘗試。** 通過對輸入矩陣進行一些修改,可以輕松地將問題轉換為矩陣轉置。 如果我們反轉輸入矩陣中存在的每個偶數行,則可以使用給定[(此處](https://www.geeksforgeeks.org/inplace-m-x-n-size-matrix-transpose/))的解決方案按所需順序轉換矩陣,并且也無需使用任何輔助存儲器。 以下是該想法的 C++ 實現。 ``` // Program for convert matrix in specific order // using in-place matrix transpose #include <bits/stdc++.h> #define HASH_SIZE 128 using namespace std; // Non-square matrix transpose of matrix of size r x c // and base address A void transformMatrix(int *A, int r, int c) { ????// Invert even rows ????for (int i = 1; i < r; i = i + 2) ????????for (int j1 = 0, j2 = c - 1; j1 < j2; j1++, j2--) ????????????swap(*(A + i*c + j1), *(A + i*c + j2)); ????// Rest of the code is from below post ????// http://tinyurl.com/j79j445 ????int size = r*c - 1; ????int t; // holds element to be replaced, eventually ???????????// becomes next element to move ????int next; // location of 't' to be moved ????int cycleBegin; // holds start of cycle ????bitset<HASH_SIZE> b; // hash to mark moved elements ????b.reset(); ????b[0] = b[size] = 1; ????int i = 1; // Note that A[0] and A[size-1] won't move ????while (i < size) ????{ ????????cycleBegin = i; ????????t = A[i]; ????????do ????????{ ????????????// Input matrix [r x c] ????????????// Output matrix 1 ????????????// i_new = (i*r)%(N-1) ????????????next = (i*r)%size; ????????????swap(A[next], t); ????????????b[i] = 1; ????????????i = next; ????????}? while (i != cycleBegin); ????????// Get Next Move (what about querying ????????// random location?) ????????for (i = 1; i < size && b[i]; i++) ????????????; ????} } // A utility function to print a 2D array of size // nr x nc and base address A void Print2DArray(int *A, int nr, int nc) { ????for (int r = 0; r < nr; r++) ????{ ????????for (int c = 0; c < nc; c++) ????????????printf("%4d", *(A + r*nc + c)); ????????printf("\n"); ????} ????printf("\n"); } // Driver program to test above function int main(void) { ????int A[][4] = {{1, 2, 3, 4}, ?????????????????{5, 6, 7, 8}, ?????????????????{9, 10, 11, 12}}; ????int r = 3, c = 4; ????cout << "Given Matrix:\n"; ????Print2DArray((int *)A, r, c); ????transformMatrix((int *)A, r, c); ????cout << "Transformed Matrix:\n"; ????Print2DArray((int *)A, c, r); ????return 0; } ``` 輸出: ``` Given Matrix: 1 2 3 4 5 6 7 8 9 10 11 12 Transformed Matrix: 1 8 9 2 7 10 3 6 11 4 5 12 ```
                  <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>

                              哎呀哎呀视频在线观看