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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 圓形矩陣(以螺旋方式構造數字 1 到 m * n 的矩陣) > 原文: [https://www.geeksforgeeks.org/circular-matrix-construct-a-matrix-with-numbers-1-to-mn-in-spiral-way/](https://www.geeksforgeeks.org/circular-matrix-construct-a-matrix-with-numbers-1-to-mn-in-spiral-way/) 給定兩個值 m 和 n,以自然(從 1 到 m * n)的螺旋(或圓形)方式(順時針)填充大小為“ m * n”的矩陣。 **示例**: ``` Input : m = 4, n = 4 Output : 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 Input : m = 3, n = 4 Output : 1 2 3 4 10 11 12 5 9 8 7 6 ``` 這個想法是基于[以螺旋形式](https://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/)打印給定的矩陣。 我們創建一個大小為 m * n 的矩陣,并以螺旋方式遍歷它。 在遍歷時,我們跟蹤變量“ val”以填充下一個值,我們將“ val”一個接一個地遞增,并將其值放入矩陣中。 ## C++ ```cpp // C++ program to fill a matrix with values from // 1 to n*n in spiral fashion. #include <bits/stdc++.h> using namespace std; const int MAX = 100; // Fills a[m][n] with values from 1 to m*n in // spiral fashion. void spiralFill(int m, int n, int a[][MAX]) { ????// Initialize value to be filled in matrix ????int val = 1; ????/*? k - starting row index ????????m - ending row index ????????l - starting column index ????????n - ending column index */ ????int k = 0, l = 0; ????while (k < m && l < n) ????{ ????????/* Print the first row from the remaining ??????????rows */ ????????for (int i = l; i < n; ++i) ????????????a[k][i] = val++; ????????k++; ????????/* Print the last column from the remaining ??????????columns */ ????????for (int i = k; i < m; ++i) ????????????a[i][n-1] = val++; ????????n--; ????????/* Print the last row from the remaining ???????????rows */ ????????if (k < m) ????????{ ????????????for (int i = n-1; i >= l; --i) ????????????????a[m-1][i] = val++; ????????????m--; ????????} ????????/* Print the first column from the remaining ???????????columns */ ????????if (l < n) ????????{ ????????????for (int i = m-1; i >= k; --i) ?????????????????a[i][l] = val++; ????????????l++; ????????} ????} } /* Driver program to test above functions */ int main() { ????int m = 4, n = 4; ????int a[MAX][MAX]; ????spiralFill(m, n, a); ????for (int i=0; i<m; i++) ????{ ???????for (int j=0; j<n; j++) ??????????cout << a[i][j] << " "; ???????cout << endl; ????} ????return 0; } ``` ## Java ```java // Java program to fill a matrix with values from // 1 to n*n in spiral fashion. class GFG { ????static int MAX = 100; // Fills a[m][n] with values from 1 to m*n in // spiral fashion. ????static void spiralFill(int m, int n, int a[][]) { ????????// Initialize value to be filled in matrix ????????int val = 1; ????????/*? k - starting row index ????????m - ending row index ????????l - starting column index ????????n - ending column index */ ????????int k = 0, l = 0; ????????while (k < m && l < n) { ????????????/* Print the first row from the remaining ??????????rows */ ????????????for (int i = l; i < n; ++i) { ????????????????a[k][i] = val++; ????????????} ????????????k++; ????????????/* Print the last column from the remaining ??????????columns */ ????????????for (int i = k; i < m; ++i) { ????????????????a[i][n - 1] = val++; ????????????} ????????????n--; ????????????/* Print the last row from the remaining ???????????rows */ ????????????if (k < m) { ????????????????for (int i = n - 1; i >= l; --i) { ????????????????????a[m - 1][i] = val++; ????????????????} ????????????????m--; ????????????} ????????????/* Print the first column from the remaining ???????????columns */ ????????????if (l < n) { ????????????????for (int i = m - 1; i >= k; --i) { ????????????????????a[i][l] = val++; ????????????????} ????????????????l++; ????????????} ????????} ????} ????/* Driver program to test above functions */ ????public static void main(String[] args) { ????????int m = 4, n = 4; ????????int a[][] = new int[MAX][MAX]; ????????spiralFill(m, n, a); ????????for (int i = 0; i < m; i++) { ????????????for (int j = 0; j < n; j++) { ????????????????System.out.print(a[i][j] + " "); ????????????} ????????????System.out.println(""); ????????} ????} } /* This Java code is contributed by PrinciRaj1992*/ ``` ## Python3 ```py # Python program to fill a matrix with? # values from 1 to n*n in spiral fashion. # Fills a[m][n] with values? # from 1 to m*n in spiral fashion. def spiralFill(m, n, a): ????# Initialize value to be filled in matrix. ????val = 1 ????# k - starting row index ????# m - ending row index ????# l - starting column index ????# n - ending column index ????k, l = 0, 0 ????while (k < m and l < n): ????????# Print the first row from the remaining rows. ????????for i in range(l, n): ????????????a[k][i] = val ????????????val += 1 ????????k += 1 ????????# Print the last column from the remaining columns. ????????for i in range(k, m): ????????????a[i][n - 1] = val ????????????val += 1 ????????n -= 1 ????????# Print the last row from the remaining rows. ????????if (k < m): ????????????for i in range(n - 1, l - 1, -1): ????????????????a[m - 1][i] = val ????????????????val += 1 ????????????m -= 1 ????????# Print the first column from the remaining columns. ????????if (l < n): ????????????for i in range(m - 1, k - 1, -1): ????????????????a[i][l] = val ????????????????val += 1 ????????????l += 1 # Driver program if __name__ == '__main__': ????m, n = 4, 4 ????a = [[0 for j in range(m)] for i in range(n)] ????spiralFill(m, n, a) ????for i in range(m): ????????for j in range(n): ????????????print(a[i][j], end=' ') ????????print('') # This code is contributed by Parin Shah ``` ## C# ```cs // C# program to fill a matrix with values from // 1 to n*n in spiral fashion. using System; class GFG { ????static int MAX = 100; // Fills a[m,n] with values from 1 to m*n in // spiral fashion. ????static void spiralFill(int m, int n, int[,] a) { ????????// Initialize value to be filled in matrix ????????int val = 1; ????????/*? k - starting row index ????????m - ending row index ????????l - starting column index ????????n - ending column index */ ????????int k = 0, l = 0; ????????while (k < m && l < n) { ????????????/* Print the first row from the remaining ??????????rows */ ????????????for (int i = l; i < n; ++i) { ????????????????a[k,i] = val++; ????????????} ????????????k++; ????????????/* Print the last column from the remaining ??????????columns */ ????????????for (int i = k; i < m; ++i) { ????????????????a[i,n - 1] = val++; ????????????} ????????????n--; ????????????/* Print the last row from the remaining ???????????rows */ ????????????if (k < m) { ????????????????for (int i = n - 1; i >= l; --i) { ????????????????????a[m - 1,i] = val++; ????????????????} ????????????????m--; ????????????} ????????????/* Print the first column from the remaining ???????????columns */ ????????????if (l < n) { ????????????????for (int i = m - 1; i >= k; --i) { ????????????????????a[i,l] = val++; ????????????????} ????????????????l++; ????????????} ????????} ????} ????/* Driver program to test above functions */ ????public static void Main() { ????????int m = 4, n = 4; ????????int[,] a = new int[MAX,MAX]; ????????spiralFill(m, n, a); ????????for (int i = 0; i < m; i++) { ????????????for (int j = 0; j < n; j++) { ????????????????Console.Write(a[i,j] + " "); ????????????} ????????????Console.Write("\n"); ????????} ????} } ``` ## PHP ```php <?php // PHP program to fill a matrix with values? // from 1 to n*n in spiral fashion. // Fills a[m][n] with values from 1 to? // m*n in spiral fashion. function spiralFill($m, $n, &$a) { ????// Initialize value to be filled? ????// in matrix ????$val = 1; ????/* k - starting row index ???????m - ending row index ???????l - starting column index ???????n - ending column index */ ????$k = 0; ????$l = 0; ????while ($k < $m && $l < $n) ????{ ????????/* Print the first row from ????????the remaining rows */ ????????for ($i = $l; $i < $n; ++$i) ????????????$a[$k][$i] = $val++; ????????$k++; ????????/* Print the last column from ????????the remaining columns */ ????????for ($i = $k; $i < $m; ++$i) ????????????$a[$i][$n - 1] = $val++; ????????$n--; ????????/* Print the last row from? ????????the remaining rows */ ????????if ($k < $m) ????????{ ????????????for ($i = $n - 1; $i >= $l; --$i) ????????????????$a[$m - 1][$i] = $val++; ????????????$m--; ????????} ????????/* Print the first column from ???????????the remaining columns */ ????????if ($l < $n) ????????{ ????????????for ($i = $m - 1; $i >= $k; --$i) ????????????????$a[$i][$l] = $val++; ????????????$l++; ????????} ????} } // Driver Code $m = 4; $n = 4; spiralFill($m, $n, $a); for ($i = 0; $i < $m; $i++) { ????for ($j = 0; $j < $n; $j++) ????{ ????????echo ($a[$i][$j]); ????????echo (" "); ????} ????echo ("\n"); } // This code is contributed? // by Shivi_Aggarwal ?> ``` **輸出**: ``` 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 ``` **時間復雜度**: O(m * n) **空間復雜度**: O(m * n)
                  <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>

                              哎呀哎呀视频在线观看