54321 00000 00000
54320 00000 10000
我用的用字符串拼接方法写 代码如下:
( 还有更好的办法解决这个问题吗?请大家也来写写...)
Hanoi-X8023Z
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Hanoi_X8023Z
6{
7 public class DataOrder
8 {
9 /**//// <summary>
10 /// 将n个盘从one座借助two座,移动到three座.
11 /// </summary>
12 /// <param name="n">盘子个数</param>
13 /// <param name="one">第一个标识座A</param>
14 /// <param name="two">第二个标识座B</param>
15 /// <param name="three">第三个标识座C</param>
16 void hanoi(int n, string one, string two, string three)
17 {
18 if (n == 1)
19 {
20 move(one, three);//从A座移动到C座
21 }
22 else
23 {
24 hanoi(n - 1, one, three, two);//将A上n-1个盘借助于C座先移动到B座上
25 move(one, three);
26 hanoi(n - 1, two, one, three);//将n-1个盘从B借助于A座移动到C座上
27 }
28
29 }
30
31 /**//// <summary>
32 /// 并未真正移动盘子,只是打印出移盘的方案
33 /// </summary>
34 /// <param name="x">from从哪个座开始移</param>
35 /// <param name="y">to移动到哪个座</param>
36 void move(string x, string y)
37 {
38 FormatStr(x + y);
39 Console.WriteLine(strA + "\n" + strB + "\n" + strC + "\n");
40 }
41
42 //定义三个字符串
43 private string strA = string.Empty;//A座
44 private string strB = string.Empty;//B座
45 private string strC = string.Empty;//C座
46 private int _long = 0;//初始值
47 public int strlong//盘子个数
48 {
49 set { _long = value; }
50 }
51 /**//// <summary>
52 /// 格式化字符串
53 /// </summary>
54 /// <param name="strType">字符串类型</param>
55 private void FormatStr(string strType)
56 {
57 remove0();
58 if (strType == "AB")
59 {
60 strB = strB + strA.Substring(strA.Length - 1, 1);
61 strA = strA.Substring(0, strA.Length - 1);
62 }
63 else if (strType == "AC")
64 {
65 strC = strC + strA.Substring(strA.Length - 1, 1);
66 strA = strA.Substring(0, strA.Length - 1);
67 }
68 else if (strType == "BA")
69 {
70 strA = strA + strB.Substring(strB.Length - 1, 1);
71 strB = strB.Substring(0, strB.Length - 1);
72 }
73 else if (strType == "BC")
74 {
75 strC = strC + strB.Substring(strB.Length - 1, 1);
76 strB = strB.Substring(0, strB.Length - 1);
77 }
78 else if (strType == "CA")
79 {
80 strA = strA + strC.Substring(strC.Length - 1, 1);
81 strC = strC.Substring(0, strC.Length - 1);
82 }
83 else if (strType == "CB")
84 {
85 strB = strB + strC.Substring(strC.Length - 1, 1);
86 strC = strC.Substring(0, strC.Length - 1);
87 }
88 Add0();
89 }
90
91 /**//// <summary>
92 /// 加0
93 /// </summary>
94 private void Add0()
95 {
96 strA = strA.PadRight(_long, '0');
97 strB = strB.PadRight(_long, '0');
98 strC = strC.PadRight(_long, '0');
99
100
101 //for (int i = 0; i < _long; i++)
102 // {
103 // if (i == strA.Length)
104 // strA = strA + "0";
105 // if (i == strB.Length)
106 // strB = strB + "0";
107 // if (i == strC.Length)
108 // strC = strC + "0";
109 // }
110 }
111
112 /**//// <summary>
113 /// 去0
114 /// </summary>
115 private void remove0()
116 {
117 strA = strA.Replace("0", "");
118 strB = strB.Replace("0", "");
119 strC = strC.Replace("0", "");
120 }
121
122 /**//// <summary>
123 /// 初始化字符串
124 /// </summary>
125 private void InitStr()
126 {
127 for (int i = _long; i > 0; i--)
128 {
129 strA += i.ToString();
130 strB += "0";
131 strC += "0";
132 }
133 }
134
135 static void Main(string[] args)
136 {
137 汉诺塔问题分析方法与解答#region 汉诺塔问题分析方法与解答
138 // 问题:将n个盘子从A座移动到C座
139 // 步骤1:将A座上n-1个盘借助C座先移动到B座上
140 // 步骤2:把A座上剩下的一个盘移动到C座上
141 // 步骤3:将n-1个盘从B座借助于A座移动到C座上
142 // 上面第一步与第三步,都是把n-1个盘从一个座移动到另一个座上,采取的办法是一样的,只是座的名字不同而已
143 // 对应关系如下:第一步:one--A two--B three--C;第三步:one--B two--C three--A
144
145 DataOrder DO = new DataOrder();//实例化数据排序对象
146 DO.strlong = 5;//设置初始盘子数
147 DO.InitStr();//初始化字符串
148 string A = "A";//第一个座A
149 string B = "B";//第二个座B
150 string C = "C";//第三个座C
151 int n = 5;//座上的盘子个数
152
153 DO.hanoi(n,A,B,C);//调用汉诺塔排序方法
154 Console.ReadLine();
155 #endregion
156 }
157 }
158}
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Hanoi_X8023Z
6{
7 public class DataOrder
8 {
9 /**//// <summary>
10 /// 将n个盘从one座借助two座,移动到three座.
11 /// </summary>
12 /// <param name="n">盘子个数</param>
13 /// <param name="one">第一个标识座A</param>
14 /// <param name="two">第二个标识座B</param>
15 /// <param name="three">第三个标识座C</param>
16 void hanoi(int n, string one, string two, string three)
17 {
18 if (n == 1)
19 {
20 move(one, three);//从A座移动到C座
21 }
22 else
23 {
24 hanoi(n - 1, one, three, two);//将A上n-1个盘借助于C座先移动到B座上
25 move(one, three);
26 hanoi(n - 1, two, one, three);//将n-1个盘从B借助于A座移动到C座上
27 }
28
29 }
30
31 /**//// <summary>
32 /// 并未真正移动盘子,只是打印出移盘的方案
33 /// </summary>
34 /// <param name="x">from从哪个座开始移</param>
35 /// <param name="y">to移动到哪个座</param>
36 void move(string x, string y)
37 {
38 FormatStr(x + y);
39 Console.WriteLine(strA + "\n" + strB + "\n" + strC + "\n");
40 }
41
42 //定义三个字符串
43 private string strA = string.Empty;//A座
44 private string strB = string.Empty;//B座
45 private string strC = string.Empty;//C座
46 private int _long = 0;//初始值
47 public int strlong//盘子个数
48 {
49 set { _long = value; }
50 }
51 /**//// <summary>
52 /// 格式化字符串
53 /// </summary>
54 /// <param name="strType">字符串类型</param>
55 private void FormatStr(string strType)
56 {
57 remove0();
58 if (strType == "AB")
59 {
60 strB = strB + strA.Substring(strA.Length - 1, 1);
61 strA = strA.Substring(0, strA.Length - 1);
62 }
63 else if (strType == "AC")
64 {
65 strC = strC + strA.Substring(strA.Length - 1, 1);
66 strA = strA.Substring(0, strA.Length - 1);
67 }
68 else if (strType == "BA")
69 {
70 strA = strA + strB.Substring(strB.Length - 1, 1);
71 strB = strB.Substring(0, strB.Length - 1);
72 }
73 else if (strType == "BC")
74 {
75 strC = strC + strB.Substring(strB.Length - 1, 1);
76 strB = strB.Substring(0, strB.Length - 1);
77 }
78 else if (strType == "CA")
79 {
80 strA = strA + strC.Substring(strC.Length - 1, 1);
81 strC = strC.Substring(0, strC.Length - 1);
82 }
83 else if (strType == "CB")
84 {
85 strB = strB + strC.Substring(strC.Length - 1, 1);
86 strC = strC.Substring(0, strC.Length - 1);
87 }
88 Add0();
89 }
90
91 /**//// <summary>
92 /// 加0
93 /// </summary>
94 private void Add0()
95 {
96 strA = strA.PadRight(_long, '0');
97 strB = strB.PadRight(_long, '0');
98 strC = strC.PadRight(_long, '0');
99
100
101 //for (int i = 0; i < _long; i++)
102 // {
103 // if (i == strA.Length)
104 // strA = strA + "0";
105 // if (i == strB.Length)
106 // strB = strB + "0";
107 // if (i == strC.Length)
108 // strC = strC + "0";
109 // }
110 }
111
112 /**//// <summary>
113 /// 去0
114 /// </summary>
115 private void remove0()
116 {
117 strA = strA.Replace("0", "");
118 strB = strB.Replace("0", "");
119 strC = strC.Replace("0", "");
120 }
121
122 /**//// <summary>
123 /// 初始化字符串
124 /// </summary>
125 private void InitStr()
126 {
127 for (int i = _long; i > 0; i--)
128 {
129 strA += i.ToString();
130 strB += "0";
131 strC += "0";
132 }
133 }
134
135 static void Main(string[] args)
136 {
137 汉诺塔问题分析方法与解答#region 汉诺塔问题分析方法与解答
138 // 问题:将n个盘子从A座移动到C座
139 // 步骤1:将A座上n-1个盘借助C座先移动到B座上
140 // 步骤2:把A座上剩下的一个盘移动到C座上
141 // 步骤3:将n-1个盘从B座借助于A座移动到C座上
142 // 上面第一步与第三步,都是把n-1个盘从一个座移动到另一个座上,采取的办法是一样的,只是座的名字不同而已
143 // 对应关系如下:第一步:one--A two--B three--C;第三步:one--B two--C three--A
144
145 DataOrder DO = new DataOrder();//实例化数据排序对象
146 DO.strlong = 5;//设置初始盘子数
147 DO.InitStr();//初始化字符串
148 string A = "A";//第一个座A
149 string B = "B";//第二个座B
150 string C = "C";//第三个座C
151 int n = 5;//座上的盘子个数
152
153 DO.hanoi(n,A,B,C);//调用汉诺塔排序方法
154 Console.ReadLine();
155 #endregion
156 }
157 }
158}