Difference between revisions of "Basic Pascal Tutorial/Chapter 4/Solution/zh CN"

From Free Pascal wiki
Jump to navigationJump to search
(Created page with "4Ga - 汉诺塔参考答案 (原作者: Tao Yue, 状态: 未更改) <syntaxhighlight> (* Author: Tao Yue Date: 13 July 2000 Description: Solves the Towers...")
 
m (bypass language bar/categorization template redirect [cf. discussion])
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
4Ga - 汉诺塔参考答案 (原作者: Tao Yue, 状态: 未更改)
+
{{Basic Pascal Tutorial/Chapter 4/Solution}}
  
<syntaxhighlight>
+
4Ga - 汉诺塔参考答案 (原作者: Tao Yue, 状态: 英文部分为原文/中文部分有适当增删)
 +
 
 +
<syntaxhighlight lang=pascal>
 
(* Author:    Tao Yue
 
(* Author:    Tao Yue
 
   Date:      13 July 2000
 
   Date:      13 July 2000
Line 19: Line 21:
 
procedure DoTowers (NumDiscs, OrigPeg, NewPeg, TempPeg : integer);
 
procedure DoTowers (NumDiscs, OrigPeg, NewPeg, TempPeg : integer);
 
(* Explanation of variables:
 
(* Explanation of variables:
       Number of discs -- number of discs on OrigPeg
+
       Number of discs -- number of discs on OrigPeg/NumDiscs -- 初始塔高
       OrigPeg -- peg number of the tower
+
       OrigPeg -- peg number of the tower/一开始放塔的杆子的编号(初始杆)
       NewPeg -- peg number to move the tower to
+
       NewPeg -- peg number to move the tower to/要移到的杆子的编号(目标杆)
       TempPeg -- peg to use for temporary storage
+
       TempPeg -- peg to use for temporary storage/剩余的那根杆子的编号(过度杆)
 
*)
 
*)
  
 
begin
 
begin
   (* Take care of the base case -- one disc *)
+
   (* Take care of the base case -- one disc / 解决最基本的情况——只有一个盘子*)
 
   if NumDiscs = 1 then
 
   if NumDiscs = 1 then
 
       writeln (OrigPeg, ' ---> ', NewPeg)
 
       writeln (OrigPeg, ' ---> ', NewPeg)
   (* Take care of all other cases *)
+
   (* Take care of all other cases / 解决其余情况*)
 
   else
 
   else
 
       begin
 
       begin
 
         (* First, move all discs except the bottom disc
 
         (* First, move all discs except the bottom disc
 +
            首先,把所有除了底层的圆盘移到 过度杆
 
             to TempPeg, using NewPeg as the temporary peg
 
             to TempPeg, using NewPeg as the temporary peg
 +
            亦即把 过度杆 设定为新的 目标杆
 
             for this transfer *)
 
             for this transfer *)
 
         DoTowers (NumDiscs-1, OrigPeg, TempPeg, NewPeg);
 
         DoTowers (NumDiscs-1, OrigPeg, TempPeg, NewPeg);
 
         (* Now, move the bottommost disc from OrigPeg
 
         (* Now, move the bottommost disc from OrigPeg
 +
            现在,把底层的圆盘由 原本的初始杆 移到 原本的目标杆
 
             to NewPeg *)
 
             to NewPeg *)
 
         writeln (OrigPeg, ' ---> ', NewPeg);
 
         writeln (OrigPeg, ' ---> ', NewPeg);
 
         (* Finally, move the discs which are currently on
 
         (* Finally, move the discs which are currently on
 +
            最后,将位于 过度杆 的塔移到目标杆
 
             TempPeg to NewPeg, using OrigPeg as the temporary
 
             TempPeg to NewPeg, using OrigPeg as the temporary
 +
            这时要将 原本的初始杆 作为 新的过度杆
 
             peg for this transfer *)
 
             peg for this transfer *)
 
         DoTowers (NumDiscs-1, TempPeg, NewPeg, OrigPeg)
 
         DoTowers (NumDiscs-1, TempPeg, NewPeg, OrigPeg)
Line 50: Line 57:
  
 
begin    (* Main *)
 
begin    (* Main *)
   write ('Please enter the number of discs in the tower ===> ');
+
   write ('Please enter the number of discs in the tower ===> '); {* 请输入圆盘数 ===> *}
 
   readln (numdiscs);
 
   readln (numdiscs);
 
   writeln;
 
   writeln;
Line 58: Line 65:
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Programming_Assignment_4/zh_CN|上一页]]   
+
|[[Basic Pascal Tutorial/Chapter 4/Programming Assignment/zh_CN|上一页]]   
|[[Contents/zh_CN|目录]]  
+
|[[Basic Pascal Tutorial/Contents/zh CN|目录]]  
|[[Enumerated_types|下一页]]
+
|[[Basic Pascal Tutorial/Chapter 5/Enumerated types/zh_CN|下一页]]
 
|}
 
|}
[[Category:zh]]
 

Latest revision as of 16:20, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

4Ga - 汉诺塔参考答案 (原作者: Tao Yue, 状态: 英文部分为原文/中文部分有适当增删)

(* Author:    Tao Yue
   Date:      13 July 2000
   Description:
      Solves the Towers of Hanoi
   Version:
      1.0 - original version
*)

program TowersofHanoi;

var
   numdiscs : integer;

(********************************************************)

procedure DoTowers (NumDiscs, OrigPeg, NewPeg, TempPeg : integer);
(* Explanation of variables:
      Number of discs -- number of discs on OrigPeg/NumDiscs -- 初始塔高
      OrigPeg -- peg number of the tower/一开始放塔的杆子的编号(初始杆)
      NewPeg -- peg number to move the tower to/要移到的杆子的编号(目标杆)
      TempPeg -- peg to use for temporary storage/剩余的那根杆子的编号(过度杆)
*)

begin
   (* Take care of the base case -- one disc / 解决最基本的情况——只有一个盘子*)
   if NumDiscs = 1 then
      writeln (OrigPeg, ' ---> ', NewPeg)
   (* Take care of all other cases / 解决其余情况*)
   else
      begin
         (* First, move all discs except the bottom disc
            首先,把所有除了底层的圆盘移到 过度杆
            to TempPeg, using NewPeg as the temporary peg
            亦即把 过度杆 设定为新的 目标杆
            for this transfer *)
         DoTowers (NumDiscs-1, OrigPeg, TempPeg, NewPeg);
         (* Now, move the bottommost disc from OrigPeg
            现在,把底层的圆盘由 原本的初始杆 移到 原本的目标杆
            to NewPeg *)
         writeln (OrigPeg, ' ---> ', NewPeg);
         (* Finally, move the discs which are currently on
            最后,将位于 过度杆 的塔移到目标杆
            TempPeg to NewPeg, using OrigPeg as the temporary
            这时要将 原本的初始杆 作为 新的过度杆
            peg for this transfer *)
         DoTowers (NumDiscs-1, TempPeg, NewPeg, OrigPeg)
      end
end;

(********************************************************)


begin    (* Main *)
   write ('Please enter the number of discs in the tower ===> '); {* 请输入圆盘数 ===> *}
   readln (numdiscs);
   writeln;
   DoTowers (numdiscs, 1, 3, 2)
end.     (* Main *)
上一页 目录 下一页