1.8.23

23:二维数组回形遍历

总时间限制:
1000ms
内存限制:
65536kB
描述

给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按回形从外向内顺时针顺序遍历整个数组。如图所示:

输入
输入的第一行上有两个整数,依次为row和col。
余下有row行,每行包含col个整数,构成一个二维整数数组。
(注:输入的row和col保证0 < row < 100, 0 < col < 100)
输出
按遍历顺序输出每个整数。每个整数占一行。
样例输入
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 /*2016年12月4日openjudge日常水题
 2     ————1.8.23     By Lxzy_Zby*/
 3 #include<cstdio>
 4 using namespace std;
 5 int a[105][105];
 6 int main()
 7 {
 8     int n,m,tot=0,x,y,s=0;//初始化 
 9     scanf("%d%d",&n,&m);
10     for(int i=1;i<=n;i++)
11      for(int j=1;j<=m;j++)
12         scanf("%d",&a[i][j]);
13         int l=n*m; 
14     while(tot<l)
15     {
16     s++;x=s;y=s;
17     while(y+s<=m+1&&tot<l){printf("%d\n",a[x][y]);y++;tot++;}y--;x++;//
18     while(x+s<=n+1&&tot<l){printf("%d\n",a[x][y]);x++;tot++;}x--;y--;//
19     while(y>=s&&tot<l){printf("%d\n",a[x][y]);y--;tot++;}y++;x--;//
20     while(x>s&&tot<l){printf("%d\n",a[x][y]);x--;tot++;}//
21     }
22     return 0;
23 }

 

posted @ 2016-12-04 20:02  lxzyzby  阅读(203)  评论(0)    收藏  举报