Ç - 二维数组 - 幻方4阶 [英] C - 2D Array - Magic Square order 4

查看:145
本文介绍了Ç - 二维数组 - 幻方4阶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

114 void fillDoubly(int square[20][20], int n){
115
116     int i, j, k=0, l=0, counter=0, test[400]={0}, diff=n/4-1;
117
118     for(i=0;i<n;i++) //first nested for loops for part 1)
119         for(j=0;j<n;j++){
120             counter++;
121             if( i=j || j=(n-1-i) ){
122                 {
123                     square[i][j] = counter;
124                     test[counter-1] = 1;
125                 }
126             }
127         }
128
129     for(i=n-1;i>=0;i--) // for part 2)
130         for(j=n-1;j>=0;j--){
131             if(square[i][j]==0){
132                 while(test[k]!=0){
133                     k++;
134                 }
135                 test[k]=1;
136                 square[i][j]=k+1;
137             }
138         }
139 }

所以基本上,我不得不产生幻方的4阶
即,行和列是被4整除。

So basically, I have to generate magic square's of order 4 i.e. the rows and columns are divisible by 4.

我提供的算法,这是


  1. 来遍历数组,并在对角线子集填写

  2. 向后遍历数组,并填写其余

我已经做了上述code中的4×4阵列,这延伸到8x8,12x12等。
但我的停留在第1部分)这是为在对角线子集(如8×8分填进4x4和采取对角线代替)......我不是知道如何做到这一点,只设法填补对角线本身

I've done the 4x4 array with the above code and this extends to 8x8,12x12 etc. but I'm stuck at part 1) which is to fill in the diagonal subsets(e.g. split 8x8 into 4x4 and take that diagonal instead)...I'm not sure how to do that, only managed to fill in the diagonal itself

if( i=j || j=(n-1-i) ){

tldr,以上是我用它来知道它的对角线的情况下,任何建议如何我可以的更改条件知道,如果它的对角线子集不是对角线?

感谢

推荐答案

这是我从您链接你想你的矩阵分成16个相等的子矩阵,并填写采取翻过这些子矩阵对角线教程理解。因此,对于一个8×8矩阵你想才达到:

From what I understand from the tutorial you linked you wish to split your matrix into 16 equal submatrices, and fill take the diagonals accross these submatrices. Hence for a 8x8 matrix you want to achive:

 |   0    |    1    |    2    |    3   |  _
 0001 0002 0000 0000 0000 0000 0007 0008  0
 0009 0010 0000 0000 0000 0000 0015 0016  _
 0000 0000 0019 0020 0021 0022 0000 0000  1
 0000 0000 0027 0028 0029 0030 0000 0000  _
 0000 0000 0035 0036 0037 0038 0000 0000  2
 0000 0000 0043 0044 0045 0046 0000 0000  _
 0049 0050 0000 0000 0000 0000 0055 0056  3
 0057 0058 0000 0000 0000 0000 0063 0064  _

下面的子矩阵是2×2,如果矩阵是12×12,将它细分成3×3的16子矩阵。

Here the submatrices are 2x2, if the matrix were 12x12, it would be subdivided into 16 submatrixes of 3x3.

如果您使用这些子矩阵与哪些索引找到对角线(即我== j)条可以使用前pression:

If you use these submatrices as indexes with which to find the diagonal (i.e. i==j) you can use the expression:

if( (i/w)==(j/w)  || (j/w)==(3-(i/w)))

其中, W = N / 4 ,这是你的方块子矩阵的顺序(8×8,这是2)。因此,我/ W 将注明在子矩阵(0〜3)当前矩阵指数 I 所在。

Where w = n/4, which is the order of your square submatrix (for 8x8, this is 2). So i/w will state in which submatrix (0 to 3) the current matrix index i resides.

这篇关于Ç - 二维数组 - 幻方4阶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆