Recursion 使用recurion反转打印顺序

Recursion 使用recurion反转打印顺序,recursion,printing,output,reverse,Recursion,Printing,Output,Reverse,现在,我有以下伪代码 Printstations(fastest, path, lastpath, n) print "fastest solution requires" + fastest + "time units" print "line' + lastpath +"station" + n for j = n downto 2 print "line" + path[lastpath, j] + "station" + j-1 lastpath = pat

现在,我有以下伪代码

Printstations(fastest, path, lastpath, n)
 print "fastest solution requires" + fastest + "time units"
 print "line' + lastpath +"station" + n
 for j = n downto 2
      print "line" + path[lastpath, j] + "station" + j-1
      lastpath = path[lastpath, j]
示例输出为: 最快的解决方案需要14个时间单位: 1号线4号站 2号线3号站 2号线2号站 1号线1号站

我需要使用递归反转打印输出的顺序。 基本上,我需要它来读: 最快的解决方案需要14个时间单位: 1号线1号站 2号线2号站 2号线3号站 1号线4号站

谢谢

因此,本质上,站点顺序需要从读取4变为1,变为站点1变为4,因为样本,行顺序似乎没有改变,但实际上是因为这里的行号创建了回文。我还没能把任何东西写下来。我对递归如何改变顺序感到困惑

我想到了这个:

PrintStations(fastest, path, lastpath, n)
 if n = 0
      print "fastest solution requires" + fastest + "time units"
 else
      PrintStations(fastest, path, path[lastpath, n], n-1)
      print "line" + lastpath + "station" + n

我认为这可能行得通,但还不能完全确定。

你的思路是对的。您当前的输出如下所示:

fastest solution requires 14 time units: 
    line 1, station 4 
    line 2, station 3 
    line 2, station 2 
    line 1, station 1
variable m = n
PrintStations(fastest, path, lastpath, n)
    COMMENT: execution break condition is n == 0
    if (n = m)
        COMMENT: first call
        print "fastest solution requires" + fastest + "time units"          
    if n > 0
        COMMENT: print current path and station
        print "line" + lastpath + "station" + n
        COMMENT: call print station with next/previous path to print
        PrintStations(fastest, path, path[lastpath, n], n-1)
您希望获得这样的输出:

fastest solution requires 14 time units: 
    line 1, station 1 
    line 2, station 2 
    line 2, station 3 
    line 1, station 4
为了使用递归实现这一点,您需要更改当前的解决方案:

PrintStations(fastest, path, lastpath, n)
    if n = 0
        COMMENT: incorrect, because this will be the last printed text,
        COMMENT: this line should be called once at the begining
        COMMENT: or even better before the recursive call is executed
        print "fastest solution requires" + fastest + "time units"
    else
        COMMENT: better change the order of the resursive call and the print
        PrintStations(fastest, path, path[lastpath, n], n-1)
        print "line" + lastpath + "station" + n
这将在执行结束时而不是开始时打印行更快的解决方案需要…。我假设你从n开始,而不是0,因为如果你从0开始,复活呼叫永远不会到达

使用递归的一般方法是提取调用自身的函数,并从另一个函数开始执行它,因为您需要一个起点。 您可以创建一个名为PrintSolution的函数,该函数采用与PrintStations函数相同的参数,并从内部调用PrintStations。 这也是打印最快解决方案的正确位置…文本:

COMMENT: this is now your starting point
PrintSolution(fastest, path, lastpath, n)
    print "fastest solution requires" + fastest + "time units"
    PrintStations(fastest, path lastpath, n)
您的打印站也将变得更小,更易于写入/读取:

PrintStations(fastest, path, lastpath, n)
    COMMENT: execution break condition is n == 0
    if n > 0
        COMMENT: print current path and station
        print "line" + lastpath + "station" + n
        COMMENT: call print station with next/previous path to print
        PrintStations(fastest, path, path[lastpath, n], n-1)
当然,还有其他创建递归函数的可能性。不需要第二个函数的解决方案可能如下所示:

fastest solution requires 14 time units: 
    line 1, station 4 
    line 2, station 3 
    line 2, station 2 
    line 1, station 1
variable m = n
PrintStations(fastest, path, lastpath, n)
    COMMENT: execution break condition is n == 0
    if (n = m)
        COMMENT: first call
        print "fastest solution requires" + fastest + "time units"          
    if n > 0
        COMMENT: print current path and station
        print "line" + lastpath + "station" + n
        COMMENT: call print station with next/previous path to print
        PrintStations(fastest, path, path[lastpath, n], n-1)
在这种情况下,您需要n变量的初始值,以便只打印第一行一次

复活调用相当于迭代,因为它也在给定的数据结构上按顺序操作:

print(3);

COMMENT: equivalent to for (i = 3; i <= 1; i--) => 3 -> 2 -> 1
for i = 3 downto 1
    out i

COMMENT: 3 -> 2 -> 1
print(n)
    if (n > 0)
        print(n-1);
        out n

COMMENT: reverse the print - 1 -> 2 -> 3
variable m = n
print(n)
    if (n <= m)
        print(n+1);
        out n
print(3);
注释:相当于(i=3;i 3->2->1
对于i=3到1
我
评论:3->2->1
打印(n)
如果(n>0)
打印(n-1);
外
注释:反转打印-1->2->3
变量m=n
打印(n)

如果(n到目前为止您尝试了什么?将您的代码添加到问题中。另外,第二行中的数字不是第一行中的相反数字…一种可能是使用n-1从Printstations中调用Printstations,并在n==1或0时中断执行。为了颠倒顺序,您可能需要存储原始变量n对于中断条件,在函数外部的某个位置,从x=1开始,用x+1调用Printstations,直到x==n。