59 lines
1.0 KiB
C
59 lines
1.0 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#define MAX 15
|
|||
|
|
|||
|
int Square[MAX][MAX];
|
|||
|
int N;
|
|||
|
|
|||
|
int main(){
|
|||
|
int i , j ,row=0 , col=0 , num=2;
|
|||
|
do {
|
|||
|
printf("\nEnter odd matrix size : ");
|
|||
|
scanf("%d",&N);
|
|||
|
|
|||
|
if( N % 2 == 0 || N <= 0 || N > 15){
|
|||
|
printf("should be > 0 and 15 odd number");
|
|||
|
}else{
|
|||
|
break;
|
|||
|
}
|
|||
|
}while(1);
|
|||
|
|
|||
|
while(1){
|
|||
|
Square[0][(N-1)/2] = 1;
|
|||
|
col=(N-1)/2;
|
|||
|
if((row-1)<0 && (col+1<N)){
|
|||
|
row = N-1; // <20>p<EFBFBD>G<EFBFBD><47><EFBFBD>ƶW<C6B6>X<EFBFBD>W<EFBFBD><57><EFBFBD>ɡA<C9A1>^<5E><><EFBFBD>̫<EFBFBD><CCAB>@<40><>
|
|||
|
col++;
|
|||
|
Square[row--][col--] = num;
|
|||
|
}else if ((row-1)>0 && (col+1<N)){
|
|||
|
// row--;
|
|||
|
col = 4; // <20>p<EFBFBD>G<EFBFBD>C<EFBFBD>ƶW<C6B6>X<EFBFBD><58><EFBFBD><EFBFBD><EFBFBD>ɡA<C9A1>^<5E><><EFBFBD>Ĥ@<40>C
|
|||
|
Square[row--][co--] = num;
|
|||
|
}else if((row-1)>0 && (col+1<N)){
|
|||
|
row = row-1; // <20>p<EFBFBD>G<EFBFBD><47><EFBFBD>ƶW<C6B6>X<EFBFBD>W<EFBFBD><57><EFBFBD>ɤΥk<CEA5><6B><EFBFBD>ɡA<C9A1>^<5E><><EFBFBD>U<EFBFBD>@<40><>
|
|||
|
Square[row--][col--] = num;
|
|||
|
}
|
|||
|
|
|||
|
if(Square[row][col]==0){
|
|||
|
Square[row--][col--] = num;
|
|||
|
}
|
|||
|
|
|||
|
printf("num=%d ,Square[%d][%d]=%d\n",num ,row,col,Square[row][col]);
|
|||
|
// row++, col++;
|
|||
|
num++;
|
|||
|
|
|||
|
|
|||
|
if(num>N*N)
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
// for(i=0;i<N;i++){
|
|||
|
// for(j=0;j<N;j++){
|
|||
|
// printf("%d\t",Square[i][j]);
|
|||
|
// }printf("\n");
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
return 0 ;
|
|||
|
}
|