Home > Back-end >  The small white spiral matrix for help
The small white spiral matrix for help

Time:10-28


Really don't know what is going wrong bosses help children

#include
Int main ()
{
Int I, j, n, k, l, c=1;
Int a [n] [n].
//enter
The scanf (" % d ", & amp; n);
//laps
If (n % 2==0) k=n/2;
The else k=(n - 1)/2;
//assignment
For (l=1; L<=k; L++)
{
For (I=(l - 1); i<=(n - l - 1); I++)/line/column constant change
{
A [I] [n - l]=c;
c++;
}
For (j=(n - l); J>=l; J -)//row the same column size
{
A/n - l [j]=c;
c++;
}
For (I=(n - l); I>=l; I -)/line/column constant smaller
{
A [I] [l - 1]=c;
c++;
}
For (j=(l - 1); j<=(n - 1 - l); J++)//row the same column larger
{
A [j] [l - 1]=c;
c++;
}
}
//center
If (n % 2!=0)
{
A [(n - 1)/2] [(n - 1)/2]=c;
}
//output
for(i=0; i<=(n - 1); I++)
{
for(j=0; j<=(n - 1); J++)
{
If (j)==(n - 1) printf (" % d \ n ", a [I] [j]);
The else printf (" % d ", a [I] [j]);
}
}
return 0;
}

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
# define MAXN 100
[MAXN int m + 2] [MAXN + 2];
Char d;
Int x, y, k, n, w.
Char STR [10].
Void main () {
While (1) {
Printf (" Input n (1.. : % d) ", MAXN);
The fflush (stdout);
The rewind (stdin);
If (1==the scanf (" % d ", & amp; N)) {
If (1 & lt;=n & amp; & n<=MAXN) break;
}
}
Y=0; For (x=0; x<=n + 1; X++) m [y] [x]=1;
Y=n + 1; For (x=0; x<=n + 1; X++) m [y] [x]=1;
X=0; For (y=0; Y<=n + 1; Y++) m [y] [x]=1;
X=n + 1; For (y=0; Y<=n + 1; Y++) m [y] [x]=1;
For (y=1; Y<=n; Y++) {
For (x=1; x<=n; X++) {
M [y] [x]=0;
}
}
X=1;
Y=1;
k=0;
D='d';
While (1) {
k++;
If (k> N * n) break;
M [y] [x]=k;
The switch (d) {
Case 'D' :
If (0==m [y + 1], [x]) y++;
The else {x++; D='R'. }
break;
Case "R" :
If (0==m [y] [x + 1]) x++;
The else {y -; D='U'; }
break;
Case 'U' :
If (0==m [1] y [x]) - y;
The else {x -; D='L'. }
break;
Case 'L' :
If (0==m [y] [1] x) x -;
The else {y++; D='d'; }
break;
}
}
W=sprintf (STR, "% d", n * n);
For (y=1; Y<=n; Y++) {
For (x=1; x<=n; X++) {
Printf (" % 0 * d ", w, m [y] [x]).
}
printf("\n");
}
}

CodePudding user response:

The run results to look at, look at what your bug
, wouldn't you an array of application for failure? N you have no initialization, used to define a 2 d array directly no problem? Put it behind the scanf application
  • Related