撞墙法输出回型字符矩阵,麻烦纠错
#include<stdio.h>
#define N 15
void main()
{
int i=0,j=0,k,m,top=0,left=-1,right,botton,d=0,n;
char c,p,a[N][N];
scanf("%d %c",&n,&c);
p=c;
if(n%2==0)
{
printf("%d is not a odd number!\n",n);goto end;
}
else
{
m=n/2;
for(i=0;i<m;i++)
p=p-1;
right=n,botton=n;
for(k=1;k<=n*n;k++)
{
if(d==0)
{
if(j==right)
{ j--;
i++;
k--;
d=1;
right--;
continue;
}
else
{ a[i][j]=p;
j++;
}
}
if(d==1)
{
if(i==botton)
{
i--;
j--;
k--;
d=2;
botton--;
continue;
}
else
{ a[i][j]=p;
i++;
}
}
if(d==2)
{
if(j==left)
{ j++;
i--;
k--;
d=3;
left++;
continue;
}
else
{ a[i][j]=p;
j--;
}
}
if(d==3)
{
if(i==top)
{ i++;
j++;
k--;
d=0;
top++;
p++;
continue;
}
else
{ a[i][j]=p;
i--;
}
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%-2c",a[i][j]);
printf("\n");
}
end:;
}
[解决办法]
i初始化应该是 0.
但是在
for(i=0;i<m;i++)
p=p-1;
被改变了.之后应该恢复
下面是我调试的代码.应该无误
#include<stdio.h>#define N 15int main(){ int i = 0, j = 0, k, m, top = 0, left = -1, right, botton, d = 0, n; char c, p, a[N][N]={0}; scanf("%d %c", &n, &c); p = c; if(n % 2 == 0) { printf("%d is not a odd number!\n", n); goto end; } else { m = n / 2; // for(i = 0; i < m; i++) p = p - m; right = n, botton = n; for(k = 0; k < n * n; k++) { if(d == 0) { if(j == right) { i++; j--; k--; d = 1; right--; continue; } else { a[i][j] = p; j++; printf("i=%d,j=%d,p:%c,d=%d,right=%d,k=%d\n",i,j,p,d,right,k); } } if(d == 1) { if(i == botton) { i--; j--; k--; d = 2; botton--; continue; } else { a[i][j] = p; i++; printf("i=%d,j=%d,p:%c,d=%d,botton=%d,k=%d\n",i,j,p,d,botton,k); } } if(d == 2) { if(j == left) { j++; i--; k--; d = 3; left++; continue; } else { a[i][j] = p; j--; printf("i=%d,j=%d,p:%c,d=%d,left=%d,k=%d\n",i,j,p,d,left,k); } } if(d == 3) { if(i == top) { i++; j++; k--; d = 0; top++; p++; continue; } else { a[i][j] = p; i--; printf("i=%d,j=%d,p:%c,d=%d,top=%d,k=%d\n",i,j,p,d,top,k); } } } } for(i = 0; i < n; i++) { for(j = 0; j < n; j++) printf("%-2c", a[i][j]); printf("\n"); }end: ;}