给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按回形从外向内顺时针顺序遍历整个数组。如图所示:
4 4 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int a[100][100]; 7 int b[100][100]; 8 int main() 9 { 10 int m,n,i,j; 11 cin>>m>>n; 12 for(i=1;i<=m;i++) 13 { 14 for(j=1;j<=n;j++) 15 { 16 cin>>a[i][j]; 17 } 18 } 19 for(int q=1;q<=n/2+1;q++) 20 { 21 for(i=q;i<=m-q+1;i++) 22 { 23 for(j=q;j<=n-q+1;j++) 24 { 25 if(i==q||j==n-q+1) 26 { 27 cout<<a[i][j]<<endl; 28 b[i][j]=1; 29 } 30 } 31 } 32 for(i=m-q+1;i>=q;i--) 33 { 34 for(j=n-q+1;j>=q;j--) 35 { 36 if((i==m-q+1||j==q)&&b[i][j]==0) 37 { 38 cout<<a[i][j]<<endl; 39 } 40 } 41 } 42 } 43 }
原文:http://www.cnblogs.com/lyqlyq/p/6659662.html