一、幻方按照阶数可分成了三类,即奇数阶幻方、双偶阶幻方、单偶阶幻方。

二、奇数阶幻方(劳伯法)

奇数阶幻方最经典的填法是罗伯法。填写的方法是:

把1(或最小的数)放在第一行正中;按以下规律排列剩下的(n×n-1)个数:

(1)每一个数放在前一个数的右上一格;

(2)如果这个数所要放的格已经超出了顶行那么就把它放在底行,仍然要放在右一列;

(3)如果这个数所要放的格已经超出了最右列那么就把它放在最左列,仍然要放在上一行;

(4)如果这个数所要放的格已经超出了顶行且超出了最右列,那么就把它放在底行且最左列;

(5)如果这个数所要放的格已经有数填入,那么就把它放在前一个数的下一行同一列的格内。

例,用该填法获得的5阶幻方:

17

24

1

8

15

23

5

7

14

16

4

6

13

20

22

10

12

19

21

3

11

18

25

2

9

二、双偶数阶幻方(海尔法)

所谓双偶阶幻方就是当n可以被4整除时的偶阶幻方,即4K阶幻方。在说解法之前我们先说明一个“互补数”定义:就是在n阶幻方中,如果两个数的和等于幻方中最大的数与1的和(即n×n+1),我们称它们为一对互补数。如在三阶幻方中,每一对和为10的数,是一对互补数 ;在四阶幻方中,每一对和为17的数,是一对互补数。

双偶数阶幻方最经典的填法是海尔法。填写的方法是:

以8阶幻方为例:

(1)先把数字按顺序填。然后,按4×4把它分割成4块(如图)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

(2)每个小方阵对角线上的数字(如左上角小方阵部分),换成和它互补的数。

64

2

3

61

60

6

7

57

9

55

54

12

13

51

50

16

17

47

46

20

21

43

42

24

40

26

27

37

36

30

31

33

32

34

35

29

28

38

39

25

41

23

22

44

45

19

18

48

49

15

14

52

53

11

10

56

8

58

59

5

4

62

63

1

三、单偶数阶幻方(斯特拉兹法)

所谓单偶阶幻方就是当n不可以被4整除时的偶阶幻方,即4K+2阶幻方。如(n=6,10,14……)的幻方。

单偶数阶幻方最经典的填法是斯特拉兹法。填写的方法是:

以10阶幻方为例。这时,k=2。

(1)把魔方阵分为A,B,C,D四个象限,这样每一个象限肯定是奇数阶。用罗伯法,依次在A象限,D象限,B象限,C象限按奇数阶幻方的填法填数。

(2)在A象限的中间行、中间格开始,按自左向右的方向,标出k格。A象限的其它行则标出最左边的k格。将这些格,和C象限相对位置上的数互换位置。

(3)在B象限所有行的中间格,自右向左,标出k-1格。(注:6阶幻方由于k-1=0,所以不用再作B、D象限的数据交换),将这些格,和D象限相对位置上的数互换位置。

以上内容来源:http://www.cnblogs.com/panlijiao/archive/2012/05/11/2496757.html

实现代码如下:

1 #include

2 #include

3 #include

4

5 #define COL 20

6 #define ROW 20

7

8 void deal_argv(int argc, char **argv, int *degree) {9 if (argc != 2) {10 printf("cmd: ./a.out degree\n");11 exit(-1);12 } else{13 *degree = atoi(argv[1]);14 if (*degree <= 2 || *degree > 20) {15 printf("the degree is between 3 and 20\n");16 exit(-1);17 }18 }19 }20

21 void show_array(int (*array)[ROW], intdegree) {22 introw, col;23 for (row = 0; row < degree; row++){24 for (col = 0; col < degree; col++)25 printf("%5d", array[row][col]);26 printf("\n");27 }28 }29

30 void init_array(int (*array)[ROW], intsize) {31 memset(array, 0, size);32 }33

34 void odd_num_magic_square(int degree, int (*array)[ROW], int x, int y, intnum) {35 int element = 0;36 int col = 0;37 int row = degree / 2;38

39 for (element = num; element <= degree * degree + num - 1; element++) {40 array[col + x][row + y] =element;41 if (array[(col - 1 + degree) % degree + x][(row + 1) % degree + y] != 0) {42 col = (col + 1 + degree) %degree;43 } else{44 row = (row + 1) %degree;45 col = (col - 1 + degree) %degree;46 }47 }48 }49

50 void fill_array(int (*array)[ROW], intdegree) {51 introw, col;52 int num = 1;53

54 for (col = 0; col < degree; col++)55 for (row = 0; row < degree; row++)56 array[col][row] = num++;57 }58

59 void double_magic_square(int degree, int (*array)[ROW]) {60 int complement = 0;61 int deg = degree / 4;62 introw, col;63

64 fill_array(array, degree);65 complement = degree * degree + 1;66

67 for (col = 0; col < deg; col++) {68 for (row = 0; row < deg; row++) {69 array[col * 4][row * 4] = complement - array[col * 4][row * 4];70 array[col * 4 + 1][row * 4 + 1] = complement - array[col * 4 + 1][row * 4 + 1];71 array[col * 4 + 2][row * 4 + 2] = complement - array[col * 4 + 2][row * 4 + 2];72 array[col * 4 + 3][row * 4 + 3] = complement - array[col * 4 + 3][row * 4 + 3];73

74 array[col * 4 + 3][row * 4] = complement - array[col * 4 + 3][row * 4];75 array[col * 4 + 2][row * 4 + 1] = complement - array[col * 4 + 2][row * 4 + 1];76 array[col * 4 + 1][row * 4 + 2] = complement - array[col * 4 + 1][row * 4 + 2];77 array[col * 4][row * 4 + 3] = complement - array[col * 4][row * 4 + 3];78 }79 }80 }81

82 void change_value(int *value_a, int *value_b) {83 inttmp;84 tmp = *value_a;85 *value_a = *value_b;86 *value_b =tmp;87 }88

89 void single_magic_square(int degree, int (*array)[ROW]) {90 int deg = degree / 2;91 int k = 0;92 introw, col;93 int tmp_row = 0;94

95 odd_num_magic_square(deg, array, 0, 0, 1);96 odd_num_magic_square(deg, array, deg, deg, deg * deg + 1);97 odd_num_magic_square(deg, array, 0, deg, deg * deg * 2 + 1);98 odd_num_magic_square(deg, array, deg, 0, deg * deg * 3 + 1);99

100 k = (degree - 2) / 4;101 for (row = 0; row < k; row++) {102 for (col = 0; col < deg; col++) {103 if (col == deg / 2) {104 change_value(&array[col][deg / 2 + row], &array[col + deg][deg / 2 +row]);105 } else{106 change_value(&array[col][row], &array[col +deg][row]);107 }108 }109 }110

111 for (row = 0; row < k - 1; row++) {112 for (col = 0; col < deg; col++) {113 tmp_row = row + deg + deg / 2 + 1 - k + 1;114 change_value(&array[col][tmp_row], &array[col +deg][tmp_row]);115 }116 }117

118 }119

120

121 int main(int argc, char *argv[]) {122 intarray[COL][ROW];123 int degree = 0;124

125 deal_argv(argc, argv, &degree);126

127 init_array(array, sizeof(array));128 if ((degree % 2) != 0) {129 odd_num_magic_square(degree, array, 0, 0, 1);130 show_array(array, degree);131 } else if (degree % 4 == 0) {132 double_magic_square(degree, array);133 show_array(array, degree);134 } else{135 single_magic_square(degree, array);136 show_array(array, degree);137 }138

139

140 return 0;141 }

c 语言奇数幻方代码,【C】——幻方算法(示例代码)相关推荐

  1. cart算法示例代码

    以下是基于sklearn库的CART算法示例代码.通过构建决策树(采用Gini作为指标)对随机生成(通过np.random.randint方法)的数字进行分类,自变量X为100×4的矩阵,随机生成的数 ...

  2. html风车相册代码,Css Html 大风车(示例代码)

    简介这篇文章主要介绍了Css Html 大风车(示例代码)以及相关的经验技巧,文章约3675字,浏览量138,点赞数2,值得参考! div{ border-radius: 50%;position: ...

  3. linux mv编写代码,Linux命令--mv(示例代码)

    简介这篇文章主要介绍了Linux命令--mv(示例代码)以及相关的经验技巧,文章约4069字,浏览量253,点赞数7,值得推荐! Linux--mv mv经常被用来做备份 命令参数: -b :若需覆盖 ...

  4. ABOV单片机内部中断优先级寄存器IP1x/IPx的设置代码实现讲解及示例代码-[MC96F6332D]

    一.准备工作 1.KEIL C51编译环境 2.外部中断EINT示例代码-MC96F6332D 3.MC96F6332D 开发板 4.USB-OCD II仿真器 二.代码部分 1.现代单片机MC96F ...

  5. 手机号码吉凶算法C语言代码,手机号码测吉凶示例代码

    package api.binstd.mobileluck; import api.util.HttpUtil; import net.sf.json.JSONObject; public class ...

  6. c语言快速排序算法代码,c语言快速排序算法示例代码分享

    #include #include #include #define RANDOM(i) (rand()%i) #define N 9    //设置数组长度 //分区操作 int Partition ...

  7. topsis法matlab程序,TOPSIS算法(示例代码)

    title: TOPSIS算法 date: 2020-02-24 11:18:06 categories: 数学建模 tags: [评价模型, MATLAB] mathjax: true 定义 ? C ...

  8. C语言循环选择还有,C语言第五讲,语句 顺序循环选择.(示例代码)

    C语言第五讲,语句 顺序循环选择. 一丶语句的简明了解 我们知道,在编写C语言程序的时候,代码是顺序执行的. 从上往下执行. 但是我们可以控制流程的. 在控制之前,我们要先熟悉什么是语句. 相比大家学 ...

  9. c语言无视数据类型字符串存储,C语言基础-第二课-数据类型与运算符(示例代码)...

    1   C语言中的数据类型 1.1   常量 常量就是在程序中不可变化的量,常量在定义的时候必须给一个初值. 1.1.1#define 定义一个宏常量 1.1.2const 定义一个const常量 1 ...

最新文章

  1. 宁愿“大小周”、每天只写 200 行代码、月薪 8k-17k 人群再涨!揭晓中国开发者真实现状...
  2. 治堵有智慧 城市轨道交通建设开启奔跑模式
  3. 《零基础看得懂的C语言入门教程 》——(四)C语言的基本数据类型及变量
  4. js在类的方法中访问自己的属性
  5. 关于注册中心的CAP定理。
  6. Exchange Server 2013 OWA IIS重定向
  7. 请设计输出实数的格式,包括:⑴一行输出一个实数;⑵一行内输出两个实数;⑶一行内输出三个实数。实数用“6.2f“格式输出。
  8. 于歆杰pdf 电路原理_buck电路原理(于歆杰 电路原理pdf)
  9. Java计算当前时间,结合时区
  10. 全渠道营销与多渠道营销:定义、比较、示例
  11. python进阶练习题:IRR计算 - 盈利能力的评价【难度:2级】--景越Python编程实例训练营,不同难度Python习题,适合自学Python的新手进阶
  12. 前端:注册校验页面(html+css+javascript)
  13. ssh登陆之忽略known_hosts文件
  14. 计算机主机通电启动不了,电脑不通电,开机没反应?这里有问题!
  15. 量化交易之单因子策略
  16. 关于TTS SpeechVoiceSpeakFlags几个值的中文意思?
  17. 二本院校学弟大二开始实习,大三收割阿里、腾讯实习offer
  18. 知乎:为什么我的成绩那么好,最终还是成了一个没用的人
  19. STM32时钟系统(学习笔记之二)
  20. 如何将eclipse设置全黑背景!

热门文章

  1. vscode遇到无法访问此网站问题的两种解决方法
  2. 达梦V8归档备份参数NOT BACKED UP的使用
  3. 基于微软DEVCON的[一键禁用前面板插孔检测]程序
  4. 创建微信订阅号全攻略
  5. html做手机锁屏,手机个性锁屏怎么做?教你如何制作DIY手机锁屏
  6. RESTE MASTER和reset slave
  7. 网易互娱 实习生招聘 内推
  8. [推荐系统]推荐系统实践Reference
  9. xaxis python_Python Matplotlib.axes.Axes.invert_xaxis()用法及代码示例
  10. 备份微信聊天记录为 txt 格式保存(免 root)