1
2 static void Fun(int n)
3 {
4 System.Diagnostics.Debug.Assert(n > 0);
5 Console.WriteLine("n={0}", n);
6 var l = n + 1;
7 var r = l % 2;
8 var h = (l + r) / 2;
9 for (int i = 1; i <= h; i++)
10 {
11 StringBuilder line = new StringBuilder();
12 for (int j = 1; j <= i; j++)
13 {
14 line.Append(j);
15 line.Append(' ');
16 }
17 for (int j = l - 2 * i + r; j > 0; j--)
18 {
19 line.Append(i);
20 line.Append(' ');
21 }
22 for (int j = i-r; j > 0; j--)
23 {
24 line.Append(j);
25 line.Append(' ');
26 }
27 line.Remove(line.Length - 1, 1);
28 Console.WriteLine(line.ToString());
29 }
30 for (int i = h-r; i > 0; i--)
31 {
32 StringBuilder line = new StringBuilder();
33 for (int j = 1; j <= i; j++)
34 {
35 line.Append(j);
36 line.Append(' ');
37 }
38 for (int j = l - 2 * i + r; j > 0; j--)
39 {
40 line.Append(i);
41 line.Append(' ');
42 }
43 for (int j =i- r; j > 0; j--)
44 {
45 line.Append(j);
46 line.Append(' ');
47 }
48 line.Remove(line.Length - 1, 1);
49 Console.WriteLine(line.ToString());
50 }
51 }
1 static void Main(string[] args)
2 {
3 for (int i = 1; i < 5; i++)
4 {
5 Perform(i);
6 Console.WriteLine("------------------");
7 }
8 Console.ReadLine();
9 }
10
11 static void Perform(int n)
12 {
13 for (int i = 1; i <= n; i++)
14 {
15 for (int j = 1; j <= n; j++)
16 {
17 Perform(i, j);
18 }
19 for (int j = n - 1; j >= 1; j--)
20 {
21 Perform(i, j);
22 }
23 Console.WriteLine();
24 }
25 for (int i = n - 1; i >= 1; i--)
26 {
27 for (int j = 1; j <= n; j++)
28 {
29 Perform(i, j);
30 }
31 for (int j = n - 1; j >= 1; j--)
32 {
33 Perform(i, j);
34 }
35 Console.WriteLine();
36 }
37 }
38
39 static void Perform(int i, int j)
40 {
41 Console.Write(System.Math.Min(i, j));
42 Console.Write('\t');
43 }
呵呵,我喜欢这样的题目!