<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/magic-square/](https://www.geeksforgeeks.org/magic-square/) 階數為 n 的[幻方](http://en.wikipedia.org/wiki/Magic_square)是正方形中 n ^ 2 個數字(通常是不同整數)的排列,以使所有行,所有列以及兩個對角線中的 n 個數字總和為 相同的常數。 幻方包含從 1 到 n ^ 2 的整數。 每行,每一列和對角線中的常數之和稱為[魔術常數或魔術之和](http://en.wikipedia.org/wiki/Magic_constant)M。正常魔術方陣的魔術常數僅取決于 n,并且具有以下值: M = n(n ^ 2 + 1)/ 2 ``` For normal magic squares of order n = 3, 4, 5, ..., the magic constants are: 15, 34, 65, 111, 175, 260, ... ``` 在本文中,我們將討論如何以編程方式生成大小為 n 的幻方。 在繼續之前,請考慮以下示例: ``` Magic Square of size 3 ----------------------- 2 7 6 9 5 1 4 3 8 Sum in each row & each column = 3*(3^2+1)/2 = 15 Magic Square of size 5 ---------------------- 9 3 22 16 15 2 21 20 14 8 25 19 13 7 1 18 12 6 5 24 11 10 4 23 17 Sum in each row & each column = 5*(5^2+1)/2 = 65 Magic Square of size 7 ---------------------- 20 12 4 45 37 29 28 11 3 44 36 35 27 19 2 43 42 34 26 18 10 49 41 33 25 17 9 1 40 32 24 16 8 7 48 31 23 15 14 6 47 39 22 21 13 5 46 38 30 Sum in each row & each column = 7*(7^2+1)/2 = 175 ``` 您是否找到了存儲數字的任何模式? 在任何幻方中,第一個數字(即 1)存儲在位置(n / 2,n-1)。 將此位置設為(i,j)。 下一個數字存儲在位置(i-1,j + 1),在這里我們可以將每行&列視為圓形數組,即它們環繞。 **三個條件成立**: 1.下一個數字的位置是通過將前一個數字的行號減 1,再將前一個數字的列號增 1 來計算的。在任何時候,如果計算出的行位置變為-1,它將繞回 n- 1。 同樣,如果計算出的列位置變為 n,則它將環繞為 0。 2.如果魔方在計算位置處已經包含數字,則計算列位置將減少 2,計算行位置將增加 1。 3.如果計算的行位置是-1 &,計算的列位置是 n,則新位置將是:(0,n-2)。 ``` Example: Magic Square of size 3 ---------------------- 2 7 6 9 5 1 4 3 8 Steps: 1\. position of number 1 = (3/2, 3-1) = (1, 2) 2\. position of number 2 = (1-1, 2+1) = (0, 0) 3\. position of number 3 = (0-1, 0+1) = (3-1, 1) = (2, 1) 4\. position of number 4 = (2-1, 1+1) = (1, 2) Since, at this position, 1 is there. So, apply condition 2. new position=(1+1,2-2)=(2,0) 5\. position of number 5=(2-1,0+1)=(1,1) 6\. position of number 6=(1-1,1+1)=(0,2) 7\. position of number 7 = (0-1, 2+1) = (-1,3) // this is tricky, see condition 3 new position = (0, 3-2) = (0,1) 8\. position of number 8=(0-1,1+1)=(-1,2)=(2,2) //wrap around 9\. position of number 9=(2-1,2+1)=(1,3)=(1,0) //wrap around ``` 基于上述方法,下面是工作代碼: ## C++ ```cpp // C++ program to generate odd sized magic squares? #include <bits/stdc++.h> using namespace std; // A function to generate odd sized magic squares? void generateSquare(int n)? {? ????int magicSquare[n][n];? ????// set all slots as 0? ????memset(magicSquare, 0, sizeof(magicSquare));? ????// Initialize position for 1? ????int i = n/2;? ????int j = n-1;? ????// One by one put all values in magic square? ????for (int num = 1; num <= n*n; )? ????{? ????????if (i == -1 && j == n) //3rd condition? ????????{? ????????????j = n-2;? ????????????i = 0;? ????????}? ????????else ????????{? ????????????// 1st condition helper if next number? ????????????// goes to out of square's right side? ????????????if (j == n)? ????????????????j = 0;? ????????????// 1st condition helper if next number? ????????????// is goes to out of square's upper side? ????????????if (i < 0)? ????????????????i = n - 1;? ????????}? ????????if (magicSquare[i][j]) //2nd condition? ????????{? ????????????j -= 2;? ????????????i++;? ????????????continue;? ????????}? ????????else ????????????magicSquare[i][j] = num++; //set number? ????????j++; i--; //1st condition? ????}? ????// Print magic square? ????cout<<"The Magic Square for n="<<n<<":\nSum of " ????"each row or column "<<n*(n*n+1)/2<<":\n\n";? ????for (i = 0; i < n; i++)? ????{? ????????for (j = 0; j < n; j++)? ????????????cout<<magicSquare[i][j]<<" ";? ????????cout<<endl; ????}? }? // Driver code? int main()? {? ????int n = 7; // Works only when n is odd? ????generateSquare (n);? ????return 0;? }? // This code is contributed by rathbhupendra ```
                  <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>

                              哎呀哎呀视频在线观看