【问题标题】:Recursive print two lines for every call with Stairstep pattern使用阶梯模式为每个调用递归打印两行
【发布时间】:2018-03-14 01:56:28
【问题描述】:

我正在研究数据结构问题。下面列出了任务和我目前所做的。

import java.util.Scanner;
import java.util.Random; 
/**
* Recursive methods for fun & profit
* 
* @author (your name) 
* @version (a version number or a date)
*/
public class DSLab2
{
/**
 * Prints 2 lines of text for each recursive call, indicating call number 
     * (a value >= 1, and <= value of numCalls, as illustrated below.  Each
     * level of recursion should be indicated by indenting the input line by
     * r spaces.  For example, if numCalls is 3, the method should print:
 *  call 1
 *   call 2
 *    call 3
 *    back 3
 *   back 2
 *  back 1
 *  @param r the level of method calls
 *  @param numCalls the number of intended levels
 */
public static void stairSteps (int r, int numCalls)
{
    System.out.println("call "+r);
    System.out.println("back "+r);
}

我尝试了以下方法,但它只输出了阶梯要求的下半部分。我无法弄清楚如何递归调用该方法,以便在每次调用 without them being next to each other 中打印两行。

public static void stairSteps (int r, int numCalls)

if(numCalls>0)
   {

    for ( int i = 0; i <numCalls ; i++){
    System.out.print(" ");
    }
    System.out.println("back " + r);
    stairSteps(r-1, numCalls-1); 
   }
   else
   return;

有什么建议吗?

【问题讨论】:

    标签: java recursion recursive-datastructures


    【解决方案1】:
    public static void stairSteps (int r, int numCalls)
    {
        if ( numCalls==0 )
            return ;
        for (int i=0; i<r; i++)
            System.out.print(" ");
        System.out.println("call "+r);
    
        stairSteps(r+1, numCalls-1) ;
    
        for (int i=0; i<r; i++)
            System.out.print(" ");
        System.out.println("back "+r);
    }
    
    public static void main(String argv[]) {
        stairSteps(1, 4) ;
    }
    
    
    
     call 1
      call 2
       call 3
        call 4
        back 4
       back 3
      back 2
     back 1
    

    【讨论】:

    • 谢谢。但这给出了模式,但假设调用 stairSteps(5,6) 我得到以下结果,这是不正确的。 call 5 call 6 call 7 call 8 call 9 call 10 back 10 back 9 back 8 back 7 back 6 back 5 计数应该从1开始。
    • 这个怎么样。将第一个 r var 保存到全局 var gr。并打印 r-gr+1。
    【解决方案2】:

    您还应该使用 List & Stack 数据结构,如下所示。

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;
    
    public class Main {
    
        private static List<String> list = new ArrayList<>();
        private static Stack<String> stack = new Stack<>();
    
        public static void main(String[] args) throws Exception {
            stairSteps(1,3);
            for(String s:list) System.out.println(s);
            while(!stack.isEmpty()) System.out.println(stack.pop());
        }
    
    public static void stairSteps (int r, int numCalls)
    {
        if(numCalls==0) return;
        StringBuilder sb = new StringBuilder();
        for(int i=1;i<r;i++) sb.append(" ");
        list.add(sb.toString()+"Call "+r);
        stack.push(sb.toString()+"Back "+r);
        if(r<numCalls) stairSteps(r+1, numCalls);
    }
    
    }
    

    【讨论】:

    • 谢谢。我希望尽快了解这一点。但目前,我们还没有学过堆栈。
    猜你喜欢
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多