您好,登錄后才能下訂單哦!
這篇文章主要介紹C語言魔方陣的實現(xiàn)方法有哪些,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
把1到n*n排成n行n列方陣,使方陣中的每一行、每一列以及對角線上的數(shù)之和都相同,即為n階魔方陣。
根據(jù)魔方陣的規(guī)律,我將它分為三種情況。
規(guī)律:第一個數(shù)放在第一行的中間,下一個數(shù)放在上一個數(shù)的上一行下一列,若該位置已經(jīng)有了數(shù)字即放在上個數(shù)的下面一行的相同列
用C語言編程如下:
示例:n=5;
#include<stdio.h> #include<stdlib.h> #include<assert.h> void Magic1() { #define ROW 5 #define COL ROW assert(ROW % 2 != 0); //判斷n是否為奇數(shù) int arr[ROW][COL] = { 0 }; //定義二維數(shù)組 int currow = 0; int curcol = COL / 2; arr[currow][curcol] = 1; for (int i = 2; i <= ROW * COL; i++) { if (arr[(currow - 1 + ROW) % ROW][(curcol + 1) % COL] == 0) //按照規(guī)律賦值 { currow = (currow - 1 + ROW) % ROW; curcol = (curcol + 1) % COL; } else { currow = (currow + 1) % ROW; } arr[currow][curcol] = i; } for (int i = 0; i < ROW; i++) //打印魔方陣 { for (int j = 0; j < COL; j++) { printf("%-3d", arr[i][j]); } printf("\n"); } } int main() { Magic1(); return 0; }
結(jié)果:
規(guī)律:按數(shù)字從小到大,即1,2,3……n順序?qū)δХ疥噺淖蟮接遥瑥纳系较逻M(jìn)行填充;
將魔方陣分成若干個4×4子方陣(如:8階魔方陣可分成四個4×4子方陣),將子方陣對角線上的元素取出;將取出的元素按從大到小的順序依次填充到n×n方陣的空缺處。
#include<stdio.h> #include<stdlib.h> #include<assert.h> //偶數(shù)魔方陣 4K void Magic2() { #define ROW 8 #define COL ROW int tmp = 1; int arr[ROW][COL] = { 0 }; //定義二維矩陣 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { arr[i][j] = tmp++; } } int row1 = 1; int col1 = 1; int row2 = 1; int col2 = 1; for (int i = 0; i < (ROW / 4) ; i++) { for (int j = 0; j < (COL / 4); j++) { row1 = 4 * i; col1 = 4 * j; row2 = 4 * i; col2 = 4 * j + 3; for (int k = 0; k < 4; k++) { arr[row1][col1] = (ROW * COL + 1) - arr[row1][col1]; arr[row2][col2] = (ROW * COL + 1) - arr[row2][col2]; row1++; col1++; row2++; col2--; } } } for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { printf("%-3d", arr[i][j]); } printf("\n"); } } int main() { Magic2(); return 0; }
結(jié)果:
規(guī)律:
3.1.填充規(guī)則
將魔方分成A、B、C、D四個k階奇方陣, 利用奇數(shù)魔方陣填充方法依次將A、D、B、C填充 。
3.2.交換規(guī)則 上下標(biāo)記的數(shù)字進(jìn)行交換
1.右半邊大于k+2的列(從1開始)
2.左半邊,上下兩個塊最中心的點進(jìn)行交換
3.左半邊小于中心列的列(除了上下半邊最中心的行的第一列的那個值不用交換)(從1開始)
#include<stdio.h> #include<assert.h> #include<stdlib.h> void Magic3() { #define ROW 10 #define COL ROW assert(ROW % 2 == 0 && ROW % 4 != 0); int arr[ROW][COL] = { 0 }; //左上角 int currow = 0; int curcol = ROW/4; arr[currow][curcol] = 1; int tmp = 0; for (int i = 2; i <= ROW * COL/ 4; i++) { if (arr[(currow - 1 + ROW / 2) % (ROW / 2)][(curcol + 1) % (COL / 2)] == 0) //判斷上一行下一列是否被賦值 { currow = (currow - 1 + ROW / 2) % (ROW / 2); curcol = (curcol + 1) % (COL / 2); } else { currow = (currow + 1) % (ROW / 2); } arr[currow][curcol] = i; } //右下角 currow = ROW / 2; for (int i = 0; i < ROW / 2; i++, currow++) { curcol = COL / 2; for (int j = 0; j < COL / 2; j++, curcol++) { arr[currow][curcol] = arr[i][j] + 9; } } //右上角 currow = 0; for (int i = ROW/2; i < ROW ; i++, currow++) { curcol = COL / 2; for (int j = COL/2; j < COL; j++, curcol++) { arr[currow][curcol] = arr[i][j] + 9; } } //左下角 currow = ROW / 2; for (int i = 0; i < ROW/2; i++, currow++) { curcol = 0; for (int j = COL/2; j < COL; j++, curcol++) { arr[currow][curcol] = arr[i][j] + 9; } } //替換規(guī)則1:右半邊 大于k+2的列 進(jìn)行上下交換 for (int i = 0; i < ROW / 2; i++) { for (int j = ROW / 2 + ROW / 4 + 2; j < COL; j++) { tmp = arr[i][j]; arr[i][j] = arr[i + ROW / 2][j]; arr[i + ROW / 2][j] = tmp; } } //替換規(guī)則2:交換左半邊,兩個中心節(jié)點 currow = ROW / 4; curcol = COL / 4; tmp = arr[currow][curcol]; arr[currow][curcol] = arr[currow + ROW / 2][curcol]; arr[currow + ROW / 2][curcol] = tmp; //替換規(guī)則3:左半邊,除(K+1,1)這個點外,小于k+1的列 上下交換 for (int j = 0; j < ROW / 4; j++) //表示交換的列 { for (int i = 0; i < ROW / 2; i++) //表示交換的行 { if (i == ROW / 4 && j == 0) { continue; } else { tmp = arr[i][j]; arr[i][j] = arr[i + ROW / 2][j]; arr[i + ROW / 2][j] = tmp; } } } //打印 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { printf("%-3d", arr[i][j]); } printf("\n"); } } int main() { Magic3(); return 0; }
結(jié)果:
以上是“C語言魔方陣的實現(xiàn)方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。