輸出一個和螺旋數組如下:
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
這是我的源碼:
#include<stdio.h>
void main(){
int a[100][100]={0};
int n,i,j,k = 0;
scanf("%d",&n);
for(i = 1;i<=n/2;i++){
for(j = i;j<=n-i;j++)
a[j][i] = k++;
for(j = i;j<=n-i;j++)
a[n-i+1][j] = k++;
for(j = n-i+1;j>=i+1;j--)
a[j][n-i+1]=k++;
for(j = n+i-1;j>=i+1;j--)
a[i][j] = k++;
}
for(i=1;i<=n;i++){
for(j = 1 ;j<=n;j++ )
printf("%d ",a[i][j]);
printf("\n");
}
}
#include<stdio.h>
void main(){
int a[100][100]={0};
int n,i,j,k = 0;
scanf("%d",&n);
for(i = 1;i<=n/2;i++){
for(j = i;j<=n-i;j++)
a[j][i] = k++;
for(j = i;j<=n-i;j++)
a[n-i+1][j] = k++;
for(j = n-i+1;j>=i+1;j--)
a[j][n-i+1]=k++;
for(j = n-i+1;j>=i+1;j--)//修改為 j=n-i+1
a[i][j] = k++;
}
for(i=1;i<=n;i++){
for(j = 1 ;j<=n;j++ )
printf("%2d ",a[i][j]); //修改為 %2d
printf("\n");
}
}