1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 int a[100][100]; 5 int main() 6 { 7 int i,k,m,n,cnt; 8 while(~scanf("%d",&n)){ 9 cnt=1; 10 memset(a,0,sizeof(a)); 11 for(k=1; k<=(n+2)/3; k++) 12 { 13 for(i=k;i<=n-k;i++) 14 if(!a[k][i]) 15 a[k][i]=cnt++; 16 m=n-k+2; 17 for(i=k;i<=n-k;i++) 18 if(!a[i][m-i]) 19 a[i][m-i]=cnt++; 20 for(i=n-k+1;i>k;i--) 21 if(!a[i][k]) 22 a[i][k]=cnt++; 23 } 24 for(k=1;k<=n;k++){ 25 for(i=1;i<=n-k+1;i++){ 26 printf("%4d",a[k][i]); 27 } 28 printf("\n"); 29 } 30 } 31 system("pause"); 32 return 0; 33 }
每一次要填充时判定下是否已存在,即判定a[i][j]是否为0!