Z array:
# Include <stdio. h> # include <stdlib. h> void fun (int n) {/* dynamically create a two-dimensional array */INT ** A = (INT **) malloc (N * sizeof (int *)); if (A = NULL) return; int I, j; for (I = 0; I <n; I ++) A [I] = (int *) malloc (N * sizeof (INT); for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {int K = I + J; If (k <n) {int sum = K * (k + 1)/2; int index; index = (k % 2 = 0 )? J: I; A [I] [J] = sum + index;} else {int t = 2 * n-1-k; int sum = (1 + T) * T/2; sum = N * n-1-sum; int Index = (k % 2 = 0 )? (N-I): (n-j); A [I] [J] = sum + index ;}}for (I = 0; I <N; I ++) {for (j = 0; j <n; j ++) {printf ("% d \ t", a [I] [J]);} printf ("\ n") ;}/ * release */for (I = 0; I <n; I ++) Free (A [I]); free (a); return;} int main () {int N; scanf ("% d", & N); printf ("% d \ n", N ); while (n! = 0) {fun (n); scanf ("% d", & N); printf ("% d \ n", n);} return 1 ;}
Spiral array:
# Include <stdio. h> # include <stdlib. h> void fun (int n) {// dynamically create a two-dimensional array int ** arry = (INT **) malloc (N * sizeof (int *)); if (arry = NULL) return; int I, j; for (I = 0; I <n; I ++) arry [I] = (int *) malloc (N * sizeof (INT); // spiral array int K = 0, a = 1; I = J = 0; For (; k <(n + 1) /2; k ++) {While (j <n-k) arry [I] [J ++] = A ++; I ++; j --; while (I <n-k) arry [I ++] [J] = A ++; I --; j --; while (j> = K) arry [I] [j --] = A ++; I --; j ++; while (I> K) arry [I --] [J] = a + +; I ++; j ++ ;}for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) printf ("% d \ t", arry [I] [J]); printf ("\ n") ;}// release the memory for (I = 0; I <n; I ++) Free (arry [I]); free (arry); return;} int main () {int N; scanf ("% d ", & N); While (n! = 0) {fun (n); scanf ("% d", & N);} return 1 ;}