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; // 如果行數超出上邊界,回到最後一行
|
|
col++;
|
|
Square[row--][col--] = num;
|
|
}else if ((row-1)>0 && (col+1<N)){
|
|
// row--;
|
|
col = 4; // 如果列數超出左邊界,回到第一列
|
|
Square[row--][co--] = num;
|
|
}else if((row-1)>0 && (col+1<N)){
|
|
row = row-1; // 如果行數超出上邊界及右邊界,回到下一行
|
|
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 ;
|
|
}
|