Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 4/Solution/zh CN: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Basic Pascal Tutorial/Chapter 4/Solution/zh CN

OldNewLine
1+4Ga - 汉诺塔参考答案 (原作者: Tao Yue, 状态: 英文部分为原文/中文部分有适当增删)
2+
3+``` pascal
4+(* Author: Tao Yue
5+ Date: 13 July 2000
6+ Description:
7+ Solves the Towers of Hanoi
8+ Version:
9+ 1.0 - original version
10+*)
11+
12+program TowersofHanoi;
13+
14+var
15+ numdiscs : integer;
16+
17+(********************************************************)
18+
19+procedure DoTowers (NumDiscs, OrigPeg, NewPeg, TempPeg : integer);
20+(* Explanation of variables:
21+ Number of discs -- number of discs on OrigPeg/NumDiscs -- 初始塔高
22+ OrigPeg -- peg number of the tower/一开始放塔的杆子的编号(初始杆)
23+ NewPeg -- peg number to move the tower to/要移到的杆子的编号(目标杆)
24+ TempPeg -- peg to use for temporary storage/剩余的那根杆子的编号(过度杆)
25+*)
26+
27+begin
28+ (* Take care of the base case -- one disc / 解决最基本的情况——只有一个盘子*)
29+ if NumDiscs = 1 then
30+ writeln (OrigPeg, ' ---> ', NewPeg)
31+ (* Take care of all other cases / 解决其余情况*)
32+ else
33+ begin
34+ (* First, move all discs except the bottom disc
35+ 首先,把所有除了底层的圆盘移到 过度杆
36+ to TempPeg, using NewPeg as the temporary peg
37+ 亦即把 过度杆 设定为新的 目标杆
38+ for this transfer *)
39+ DoTowers (NumDiscs-1, OrigPeg, TempPeg, NewPeg);
40+ (* Now, move the bottommost disc from OrigPeg
41+ 现在,把底层的圆盘由 原本的初始杆 移到 原本的目标杆
42+ to NewPeg *)
43+ writeln (OrigPeg, ' ---> ', NewPeg);
44+ (* Finally, move the discs which are currently on
45+ 最后,将位于 过度杆 的塔移到目标杆
46+ TempPeg to NewPeg, using OrigPeg as the temporary
47+ 这时要将 原本的初始杆 作为 新的过度杆
48+ peg for this transfer *)
49+ DoTowers (NumDiscs-1, TempPeg, NewPeg, OrigPeg)
50+ end
51+end;
52+
53+(********************************************************)
54+
55+begin (* Main *)
56+ write ('Please enter the number of discs in the tower ===> '); {* 请输入圆盘数 ===> *}
57+ readln (numdiscs);
58+ writeln;
59+ DoTowers (numdiscs, 1, 3, 2)
60+end. (* Main *)
61+```
62+
63+| | | |
64+|----|----|----|
65+| [[Basic Pascal Tutorial/Chapter 4/Programming Assignment/zh CN|上一页]] | [[Basic Pascal Tutorial/Contents/zh CN|目录]] | [[Basic Pascal Tutorial/Chapter 5/Enumerated types/zh CN|下一页]] |
66+
67+---
68+*From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Solution/zh CN", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history