A topic-spiral matrix made by the first grade

xiaoxiao2021-03-06  71

/ * Spiral matrix * /

#include

#include

#define down 0

#define Left 1

#define up 2

#define right 3

// n * n matrix

#define n 5

Void PrintMatrix (int * a [], int N) {

INT I, J;

For (i = 0; i

For (j = 0; j

Printf ("% 4D", A [i] [j]);

}

Printf ("/ n");

}

}

Void Spiralmatrix (int * a [], int N) {

INT I, J; // coordinate

INT count; // counter

INT K; // cycle variable

INT DIRECT; / / Direction instruction

i = 0;

J = - N;

count = 0;

Direct = down;

While (n> 0) {

FOR (k = 0; k

a [i] [j] = count ;

Switch (Direct) {

Case Down:

i ;

Break;

Case LEFT:

J -;

Break;

Case Up:

I-;

Break;

Case Right:

J ;

Break;

}

}

// If the direction just walked is Right, step size is reduced by 2, and correct the location

IF (Direct == Right) {

i ;

J -;

n - = 2;

}

//

Direct = (Direct 1)% 4;

}

IF (n == 0) {

a [i] [j] = count;

}

}

Void main (void) {

INT M [N] [N] = {0};

INT * a [N];

INT I;

For (i = 0; i

a [i] = m [i];

}

Spiralmatrix (A, N);

PrintMatrix (A, N);

PRINTF ("Press any key to exit ...");

Getch ();

}

转载请注明原文地址:https://www.9cbs.com/read-92247.html

New Post(0)