逆时针打印矩阵

题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

例如:如果输入如下矩阵:

1              2              3              4
5              6              7              8
9              10           11           12
13           14           15           16

则依次打印出数字1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7


分析过程:思路比较简单,分为四个函数按顺序循环调用即可。

     分别是列递增,行递增,列递减,行递减。

     注意每次调用完函数后要将下个行/列总数减一。

代码如下:

代码
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 int[,] t = new int[4,4];
6 ArrayInput(t, 4, 4);
7 StdPrint(t);
8
9 ClockWisePrint(t);
10 }
11 /// <summary>
12 /// 记录当前行长度
13 /// </summary>
14   private static int CurrentRows = 0;
15 /// <summary>
16 /// 记录当前列长度
17 /// </summary>
18   private static int CurrentColums = 0;
19
20 static void ClockWisePrint(int[,]a)
21 {
22 CurrentRows = a.GetLength(0);
23 CurrentColums = a.GetLength(1);
24 ClockWiseColumnIncrease(a, 0, 0, CurrentRows);
25 }
26 /// <summary>
27 /// 列递增遍历
28 /// </summary>
29 /// <param name="a"></param>
30 /// <param name="startX"></param>
31 /// <param name="startY"></param>
32 /// <param name="length"></param>
33 static void ClockWiseColumnIncrease(int[,]a ,int startX,int startY, int length)
34 {
35 int n = 0;
36 for(n=0;n<length;n++)
37 {
38 Console.Write(a[startX + n, startY] + " ");
39 }
40 if (CurrentColums > 1)
41 {
42 ClockWiseRowIncrease(a, startX + n-1, startY + 1, --CurrentColums);
43 }
44 }
45 /// <summary>
46 /// 行递增遍历
47 /// </summary>
48 /// <param name="a"></param>
49 /// <param name="startX"></param>
50 /// <param name="startY"></param>
51 /// <param name="length"></param>
52 static void ClockWiseRowIncrease(int[,]a ,int startX,int startY, int length)
53 {
54 int i = 0;
55 for(i=0;i<length;i++)
56 {
57 Console.Write(a[startX, startY + i] + " ");
58 }
59 if (CurrentRows > 1)
60 {
61 ClockWiseColumnDecrease(a, startX - 1, startY + i-1, --CurrentRows);
62 }
63 }
64 /// <summary>
65 /// 列递减遍历
66 /// </summary>
67 /// <param name="a"></param>
68 /// <param name="startX"></param>
69 /// <param name="startY"></param>
70 /// <param name="length"></param>
71 static void ClockWiseColumnDecrease(int[,]a ,int startX,int startY, int length)
72 {
73 int i = 0;
74 for(i=0;i<length;i++)
75 {
76 Console.Write(a[startX - i, startY] + " ");
77 }
78 if (CurrentColums > 1)
79 {
80 ClockWiseRowDecrease(a, startX - i + 1, startY - 1, --CurrentColums);
81 }
82 }
83 /// <summary>
84 /// 行递减遍历
85 /// </summary>
86 /// <param name="a"></param>
87 /// <param name="startX"></param>
88 /// <param name="startY"></param>
89 /// <param name="length"></param>
90 static void ClockWiseRowDecrease(int[,]a ,int startX,int startY, int length)
91 {
92 int i = 0;
93 for(;i<length;i++)
94 {
95 Console.Write(a[startX, startY - i] + " ");
96 }
97 if (CurrentRows > 1)
98 {
99 ClockWiseColumnIncrease(a, startX + 1, startY - i + 1, --CurrentRows);
100 }
101 }
102 /// <summary>
103 /// 输入函数
104 /// </summary>
105 /// <param name="a"></param>
106 /// <param name="x"></param>
107 /// <param name="y"></param>
108 static void ArrayInput(int[,] a,int x,int y)
109 {
110 int n = 0;
111 for(int i=0;i<x;i++)
112 {
113 for (int j = 0; j < y; j++)
114 {
115 a[i, j] = ++n;
116 }
117 }
118
119 }
120 /// <summary>
121 /// 标准输出
122 /// </summary>
123 /// <param name="a"></param>
124 static void StdPrint(int[,]a)
125 {
126 for(int i=0;i<a.GetLength(0);i++)
127 {
128 for (int j=0;j<a.GetLength(1);j++)
129 {
130 Console.Write("\t" + a[i,j]);
131 }
132 Console.WriteLine();
133 }
134 }
135 }

 

posted on 2010-12-15 22:54  seek  阅读(1065)  评论(1编辑  收藏  举报

导航