一个二维数组以顺时针螺旋的方式打印出来
* 例如,对数组:
1 2 3 4 5
14 15 16 17 6
13 20 19 18 7
12 11 10 9 8
* 打印出来的序列应该是:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
程序:
#include <iostream>
using namespace std;
int main(int argc, char **argv){
int a[4][5]={{1,2,3,4,5},
{14,15,16,17,6},
{13,20,19,18,7},
{12,11,10,9,8}};
int left=0, right=4;
int low=0, high=3;
while(left<right||low<high)
{
for(int i=left; i<=right; i++){
cout<<a[low][i]<<" ";
}
low++;
for(int i=low; i<=high; i++){
cout<<a[i][right]<<" ";
}
right--;
for(int i=right; i>=left; i--){
cout<<a[high][i]<<" ";
}
high--;
for(int i=high; i>=low; i--){
cout<<a[i][left]<<" ";
}
left++;
}
return 0;
}