/*
* 。
*
* :
* 【1】 1 , ;
* 【2】 , , ;
* 【3】 , , ;
* 【4】 , 。
*/
#include <stdio.h>
#include <stdlib.h>
void magic(int n) {
//
if(n < 0 || n % 2 == 0){
return;
}
int rowSum = 0;
int i = 0,j = 0;
int ni = 0,nj = 0;
int k = 0;
//
int nn = n * n;
//
int ** magicVector = (int **)malloc(n * sizeof(int));
for(i=0;i<n;i++){
magicVector[i] = (int *)malloc(n * sizeof(int));
}
//
for(i=0;i<n;i++){
for(j=0;j<n;j++){
magicVector[i][j] = 0;
}
}
//
i = 0; j = n / 2;
for(k=1;k<=nn;k++){
magicVector[i][j] = k;
if(i == 0)
ni = n - 1;
else
ni = i - 1;
if( j == (n-1))
nj = 0;
else
nj = j + 1;
if(magicVector[ni][nj] == 0){
i = ni;
j = nj;
}else{
i++;
}
//j = nj;
}
//
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%4d",magicVector[i][j]);
}
printf("
");
}
for(i=0;i<n;i++){
rowSum += magicVector[0][i];
}
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
");
printf("The sum of row or line or cross is %d
",rowSum);
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
");
}
int main() {
int row = 0;
while(1){
printf("
Please input row or col of MagicMatrix(#0 to quit#)
");
printf(">>>");
scanf("%d",&row);
if(row == 0)
break;
magic(row);
}
return 0;
}