顺时针打印矩阵

 

 

 1 #include "stdio.h"
 2 #include <iostream> 
 3 using namespace std;
 4 #include "string.h"
 5 #include "stdlib.h"
 6 int num[1001][1001];
 7 typedef struct Lnode
 8 {
 9     int value;
10     Lnode *next;
11 };
12 
13 int main()
14 {
15     int n,i,j,k;
16     int sum=0,temp,loc;
17     int m;
18     int sx,sy,ex,ey;
19     while(scanf("%d%d",&m,&n)!=EOF)
20     {    
21         if (n<1||m<1)
22         {
23             continue;
24         }
25         for (i=0;i<m;i++)
26         {
27             for(j=0;j<n;j++)
28             {
29                 scanf("%d",&num[i][j]);
30             }
31         }
32         sx=sy=0;
33         ex=sx+n-1;
34         ey=sy+m-1;
35     
36 
37         while(sx<=ex&&sy<=ey)
38         {
39 
40                 for (i=sx;i<=ex;i++)
41                 {
42                     printf("%d ",num[sy][i]);
43                 }
44                 for (i=sy+1;i<=ey;i++)
45                 {
46                     printf("%d ",num[i][ex]);
47                 }
48                 if(ey-sy>0)              //1,2
49                 {
50                     for (i=ex-1;i>=sx;i--)
51                     {
52                         printf("%d ",num[ey][i]);
53                     }
54                 }
55                 if(ex-sx>0)                        //1
56                     for (i=ey-1;i>=sy+1;i--)              //2
57                     {
58                         printf("%d ",num[i][sx]);
59                     }
60             sx++;
61             sy++;
62             ex--;
63             ey--;
64         }
65         printf("\n");
66     }
67     
68     return 0;
69 }

 

posted on 2014-10-03 17:44  wf110  阅读(237)  评论(0编辑  收藏  举报