按照你的要求编写的C语言程序如下:
#include
int main(){
int n,i,j,k,t;
printf("输入n的值(如6):\n");
scanf("%d",&n);
int a[n][n];
if(n%2==0) t=n/2;
else t=n/2+1;
for(i=0;i
for(j=0;j
for(k=1;k<=t;k++){
if(i==k-1&&j<=n-k&&j>=k-1 || j==k-1&&i<=n-k&&i>=k-1 || i==n-k&&j<=n-k&&j>=k-1 || j==n-k&&i<=n-k&&i>=k-1){
a[i][j]=k;
}
}
}
}
for(i=0;i
for(j=0;j
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
运行结果:
输入n的值(如6):
6
1 1 1 1 1 1
1 2 2 2 2 1
1 2 3 3 2 1
1 2 3 3 2 1
1 2 2 2 2 1
1 1 1 1 1 1
#include "stdio.h"
int main(void){
int a[20][20],n,m,i,j,k;
while(1){
printf("Please enter n(1if(scanf("%d",&n) && n>1 && n<21)
break;
printf("Error, redo: ");
}
for(m=(n>>1)+(n&1),k=1;k<=m;k++)
for(i=k-1;i<=n-k;i++)
for(j=k-1;j<=n-k;j++)
if(i==k-1 || j==k-1 || i==n-k || j==n-k)
a[i][j]=k;
for(i=0;ifor(j=0;j printf("\n");
}
return 0;
}
需要编程吗