mwjhw216 发表于 2017-7-2 17:22:45

【2(2N+1)魔方阵 】

/*
2(2N+1)魔方阵
*/
#include<stdio.h>
#include<stdlib.h>
#define N 6
#define SWAP(x, y) {int t; t = x; x = y; y = t;}
void magic_o(int [], int);
void exchange(int [], int);
int main(void){
int square = {0};
int i, j;
magic_o(square, N/2);
exchange(square, N);
for(i = 0; i < N; i++){
for( j = 0; j < N; j++){
printf("%2d ", square);
}
putchar('\n');
}
return 0;
}
void magic_o(int square[], int n){
int count, row, column;
row = 0;
column = n /2;
for(count = 1; count <= n*n; count++){
square = count;
square = count + n*n;
square = count + 2*n*n;
square = count + 3*n*n;
if(count % n == 0){
row++;
}else{
row = (row == 0) ? n - 1 : row - 1;
column = (column == n - 1) ? 0 : column + 1;
}
}
}
void exchange(int x[], int n){
int i, j;
int m = n / 4;
int m1 = m - 1;
for(i = 0; i < n/2; i++){
if(i != m){
for(j = 0; j < m; j++){
SWAP(x, x);
}
for(j = 0; j < m1; j++){
SWAP(x, x);
}
}else{
for(j = 1; j <= m; j++){
SWAP(x, x);
}
for(j = 0; j <= m1; j++){
SWAP(x, x);
}
}
}
}
  运行结果:
页: [1]
查看完整版本: 【2(2N+1)魔方阵 】