2014-10-31 65 views
0

这是学校的作业。我无法理解我怎么可以打印递归如下:以间距递归地打印文字

This was written by call number 2. 
This was written by call number 3. 
    This was written by call number 4. 
    This ALSO written by call number 4. 
    This ALSO written by call number 3. 
This ALSO written by call number 2. 
This ALSO written by call number 1. 

我不知道我是否应该是说明循环与递归或是否有递归打印所有的这一种方式。此外,我将如何去逆转递归调用,以便从示例输出的4开始?

这是我目前的输出。

This was written by call number 2. 
This was written by call number 3. 
This was written by call number 4. 
This ALSO written by call number 1. 
    This ALSO written by call number 2. 
    This ALSO written by call number 3. 
    This ALSO written by call number 4. 

没有在执行for循环B/C我不知道如果这部分也应该是递归的间距。

我的代码:

public class Recursion { 

    public static void main(String[] args) { 
    for (int i = 2; i < 5; i++) { 
     System.out.println("This was written by call number " + i + "."); 
    } 
    recurse(4); 
    } 

    public static void recurse(int n) { 
    String temp = ""; 

    for (int i = 0; i < n; i++) { 
     temp += " "; 
    } 

    if (n < 2) { 
     System.out.println("This ALSO written by call number " + n + "."); 
    } 
    else { 
     recurse(n - 1); 
     System.out.println(temp + "This ALSO written by call number " + n + "."); 
    } 
} 
+0

为了使它更容易,我建议使用2个独特的功能。一个在递归调用后打印,另一个在前面打印。 – bestsss 2014-10-31 23:38:02

+0

@MichaelJames不,不需要使用2个函数。我正在编写一个答案...... – ajb 2014-10-31 23:48:17

+0

你可以用一个函数来完成它,然后可能需要硬编码4,或者将它作为第二个参数。通常我会写递归计数 - 只需要一个函数来增加计数器。无论哪种方式,递归函数都是大约4行代码。 – bestsss 2014-10-31 23:48:58

回答

1

一个简单的解决方案。

public static void main(String[] args) { 
    recurse(1); 
} 

public static void recurse (int n) { 
    if (n==5) return; 
    String temp=""; 
    for (int i=0;i<n;i++) temp += " "; 
    if (n!=1) { 
    System.out.println(temp + "This was written by call number " + n + "."); 
    } 
    recurse(n+1); 
    temp=" "; 
    for (int i=0;i<n;i++) temp += " "; 
    System.out.println(temp + "This ALSO was written by call number " + n + "."); 
} 
1

编写最递归程序(特别是你给出的分配的)关键是要查找包含了同样的问题的相似,但较小出现更大的问题。

在你的情况下,“更大的问题”是打印6行开始和结束“电话号码2”。也就是说,电话号码2到4的打印行。执行此操作的方法是:打印显示“电话号码2”的第一行,解决打印电话号码3到4的4行4到的问题,并打印最后一行说“电话号码2”。中间的部分是同一问题的发生率较小。这将是递归调用。

由于您的较大问题将以“电话号码2”开头,而您的较小问题将以较高的电话号码开头,因此我建议您安排一些事宜,以便拨打recurse(n+1)而不是recurse(n-1)。如果你这样做,你需要第二个参数,以便知道何时停止递归 - 例如recurse(n+1, last)

希望这将足以让您在正确的轨道上思考。

+0

我确实想过需要用“n + 1”来“反转我的递归”,但是我得到了一个堆栈溢出b/c,它没有第二个参数。谢谢。我会看到我能想出什么..但是,仅仅为了说明起见,我可以递归地打印第一部分:'这是通过编号n'编写的,或者我应该使用for循环,就像我目前?编辑:你说的“六条线”,所以我认为我可以做他们要求的一切for循环。无法理解我将如何使用一个功能..还有, – 2014-10-31 23:53:19

+0

@MichaelJames对于电话号码2的“大问题”是6行;较小的问题是4行。这意味着您的第2个号码的功能将打印第一行和最后一行,并在其间递归。我想如果你想一想,你会找出最后一个问题的答案。 – ajb 2014-10-31 23:58:22

+0

AH!我没有这样想过!谢谢。 – 2014-11-01 00:02:11

1

试试这个:

public static void main(String[] args) { 
    recurse(1, true, 1); 
} 

public static void recurse(int n, boolean loop, int add) { 
    String temp = ""; 
    String out = ""; 

    for (int i = 0; i < n; i++) { 
     temp += " "; 
    } 

    if (add > 0) { 
     out = temp + "This was written by call number "; 
    } else { 
     out = temp + "This ALSO written by call number "; 
    } 

    if (n == 1 && !loop) { 
     System.out.println(out + n + "."); 
     return; 
    } else if (n == 1) { 
     recurse(n+add, false, add); 
    } else if (n == 5) { 
     add = add - 2 * add; 
     recurse(n+add, false, add); 
    } else { 
     System.out.println(out + n + "."); 
     recurse(n+add, false, add); 
    } 
} 
+0

解决方案应该简单得多 – bestsss 2014-10-31 23:55:21

+0

这里已经很晚了。 :) – 2014-10-31 23:57:01

+0

从你的名字来看,这将是00:57这不是那么晚,也就是说我相信我住在你的位置东边 – bestsss 2014-10-31 23:57:52

1

这是一个非常简单的解决方案。另外请注意如何轻松获取缩进字符串(通过子字符串)。递归过程非常简单:打印数字,如果低于最大数字,则输入数字较大的函数,然后返回。

class R{ 
     static final String spaces="         "; 
     public static void main(String[] args) { 
     rec3(1,4); 
     } 
     private static void rec3(int i, int max) { 
     if (i>1) System.out.printf("%sThis was written by call number: %d%n", spaces.substring(0, i-1), i); 
     if (i<max) rec3(i+1, max); 
     System.out.printf("%sThis was ALSO written by call number: %d%n", spaces.substring(0, i-1), i);  
     } 
    } 
0

感谢大家的帮助。我最终修改了@JoseLuis的解决方案。

public class Recursion { 

    public static void main(String[] args) { 
    recurse(1, 5); 
    } 

    public static void recurse(int n, int max) { 
    String temp = ""; 
    for (int i = 0; i < n; i++) { 
     temp += " "; 
    } 
    if (n == max) { 
     return; 
    } 
    if (n != 1) { 
     System.out.println(temp + "This was written by call number " + n + "."); 
    } 
    recurse(n + 1, max); 
    System.out.println(temp + "This ALSO was written by call number " + n + "."); 
    } 
}