Data_Structure/作業/unit2/test.c

68 lines
1.3 KiB
C
Raw Normal View History

2025-01-20 21:30:53 +08:00
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int Square[MAX][MAX];
int N;
void printSquare() {
int i ,j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%d\t", Square[i][j]);
}
printf("\n");
}
}
int main() {
int row, col, num, i ,j;
do {
printf("\n<EFBFBD>п<EFBFBD><EFBFBD>J<EFBFBD>_<EFBFBD>Ƥ<EFBFBD><EFBFBD>}<7D>j<EFBFBD>p (1-15<31><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>_<EFBFBD><5F>): ");
scanf("%d", &N);
if (N % 2 == 0 || N <= 0 || N > 15) {
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>~<7E>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD>O1-15<31><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>_<EFBFBD><5F>\n");
} else {
break;
}
} while (1);
// <20><><EFBFBD>l<EFBFBD>Ƥ<EFBFBD><C6A4>}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
Square[i][j] = 0;
}
}
row = 0;
col = N / 2;
// printf("col=%d\n", col);
num = 1;
while (num <= N * N) {
Square[row][col] = num;
// <20>ˬd<CBAC>U<EFBFBD>@<40>Ӧ<EFBFBD><D3A6>m
int nextRow = (row - 1 + N) % N;
int nextCol = (col + 1) % N;
if (Square[nextRow][nextCol] != 0) {
// <20>p<EFBFBD>G<EFBFBD>U<EFBFBD>@<40>Ӧ<EFBFBD><D3A6>m<EFBFBD>w<EFBFBD>Q<EFBFBD><51><EFBFBD>ΡA<CEA1>h<EFBFBD><68><EFBFBD>U<EFBFBD><55><EFBFBD><EFBFBD>
row = (row + 1) % N;
} else {
// <20>_<EFBFBD>h<EFBFBD>A<EFBFBD><41><EFBFBD>ʨ<EFBFBD><CAA8>k<EFBFBD>W<EFBFBD><57><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>m
row = nextRow;
col = nextCol;
}
num++;
}
printf("\n%d x %d <20>]<5D><><EFBFBD>}:\n\n", N, N);
printSquare();
return 0;
}