使用递归反转打印顺序

时间:2013-10-17 20:36:54

标签: 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 = 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

我认为这可能有用,但并不完全确定。

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上。您当前的输出如下所示:

fastest solution requires 14 time units: 
    line 1, station 4 
    line 2, station 3 
    line 2, station 2 
    line 1, station 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开始,则永远不会到达resursive call。

使用递归的一般方法是提取调用自身的函数并最初从另一个函数执行它,因为您需要一个起点。 您可以创建一个名为 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 也会变得更小,更容易写/读:

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)

创建递归函数当然还有其他可能性。不需要第二个功能的解决方案可能如下所示:

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变量的初始值才能打印第一行一次。

resursive调用等同于迭代,因为它也按顺序对给定的数据结构进行操作:

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(n, m)
    if (n <= m)
        print(n+1, m);
        out n
COMMENT: initial call: print(1, 3)

如果没有关于路径数组的精确描述和更多信息(或至少数据),很难编写一个有效的解决方案,但伪代码的方向是正确的。