以递归方式打印带间距的文本

时间:2014-10-31 23:23:58

标签: java recursion

这是学校的作业。我无法理解如何以递归方式打印以下内容:

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 + ".");
    }
}

5 个答案:

答案 0 :(得分:1)

编写大多数递归程序(尤其是作为赋值给出的程序)的关键是寻找一个更大的问题,其中包含相同但较小的相同问题。

在您的情况下,&#34;更大的问题&#34;将打印以&#34;号码2和#34;开头和结尾的6行。也就是说,拨打电话号码2到4的打印行。这样做的方法是:打印第一行,表示&#34;电话号码2&#34;,解决问题,打印4行电话号码3到4 ,并打印最后一行,表示&#34;拨打电话号码2&#34;。中间的部分是同一问题的较小发生。这将成为递归电话。

由于你的大问题将从&#34;拨打电话号码2和#34;开始,而你的小问题将从一个更高的电话号码开始,我建议安排事情,以便您拨打recurse(n+1)而不是recurse(n-1)。如果你这样做,你需要第二个参数,以便你知道何时停止递归 - 类似recurse(n+1, last)

希望这足以让你思考正确的方向。

答案 1 :(得分: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);
    }
}

答案 2 :(得分: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 + ".");
}

答案 3 :(得分: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);     
      }
  }

答案 4 :(得分: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 + ".");
  }
}