2 回答
TA贡献1852条经验 获得超1个赞
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 }
- 2 回答
- 0 关注
- 746 浏览
添加回答
举报