2016-07-24 59 views
0

递归后递归的目标是打印前行的长度的一半出来,直到它到达0,然后扭转它。例如,如果原来的尺寸输入为5,结果是:如何使扭转的Java

***** 
** 
* 

* 
** 
***** 

所以我写了一个程序一样

void print(int size) { 

    if (size == 1) { 
     System.out.print("*"); 
     System.out.println();   
    } 

    if (size > 1) { 
     for (int i = 0; i < size; i++) { 
      System.out.print("*"); 
     } 
     System.out.println(); 
     size = (int)(size/2); 
     print(size); 
    } 
} 

结果出来与

***** 
** 
* 

什么使用递归来逆转结果的最好方法(第二部分)?

感谢

+0

将打印循环之后您调用''以及之前打印(大小)。 –

回答

0

您可以实现只在一个方式打印。当您的代码达到堆栈 没有任何反应。详细了解递归实际如何工作,以及当您的调用方法递归时发生了什么。

,以实现自己的目标的最佳方式(根据您的代码)是:

static void print(int size) { 
    if (size == 0) { 
     System.out.println(); 
    } else { 
     for (int i = 0; i < size; i++) { 
      System.out.print("*"); 
     } 
     System.out.println(); 

     print(size/2); 

     //when stack ends point of execution will go to next statement 
     //(stack ends when size==0, after that will be performed size==1, and size=2, size== 5, eop) 
     for (int i = 0; i < size; i++) { 
      System.out.print("*"); 
     } 
     System.out.println(); 
    } 
} 
+0

这是一个很好的答案。谢谢! – user2029709

+0

@ user2029709我很高兴能帮上忙。 –

0

您第一现有方法打印的星星,然后前往下一个递归级别,这就好比在遍历二叉树第一序遍历

要撤消结果,只需使用“后序遍历” - 先访问下一个递归级别,然后打印。

在下面的例子中,我感动print(size/2)语句后System.out.print("*")语句。它应该达到你想要的。

static void print(int size) { 
    if(size==1){ 
     System.out.print("*"); 
     System.out.println(); 
    } 

    if(size>1){ 
     print(size/2); 
     for (int i = 0; i < size; i++) { 
      System.out.print("*"); 
     } 
     System.out.println(); 
    } 
} 
0

你将不得不使用蓄电池作为身份和进行

void printReverse(int ident, int size) { 
    if (ident <= size) { 
     // quick hack , on the border case change it to the odd int 
     if (ident < size && ident * 2 > size) { 
      ident = size; 
     } 
     for (int i = 0; i < ident; i++) { 
      System.out.print("*"); 
     } 
     System.out.println(); 
     printReverse(ident * 2, size); 
    } 

} 
0

你的递归函数可以采取两个输入:

void print(int _current, int _start){ 

    if(_current < -_start){ 
     return; 
    } 

    for(int i = 0; i < Math.abs(_current); i++){ 
     System.out.print("*"); 
    } 
    System.out.println(); 
    if(_current > 0){ 
     print(_current/2, _start); 
    } else if(_current == 0){ 
     print(-1, _start); 
    } else{ 
     print(_current * 2, _start); 
    } 
} 

编辑: 在你的情况下, “主”电话将是打印(5,5);

+0

它将在第二组中打印四个星号而不是五个。 –

0

使用StringBuilder建立自己的字符串和Stack收集它们的反向输出:

static void print(int size, Stack<String> stack) { 
    if (size == 1) { 
     System.out.println("*"); 
     stack.push("*"); 
    } else if (size > 1) { 
     StringBuilder sb = new StringBuilder(size); 
     for (int i = 0; i < size; i++) { 
      sb.append("*"); 
     } 
     String s = sb.toString(); 
     System.out.println(s); 
     stack.push(s); 
     print(size/2, stack); 
    } 
} 

public static void main(String[] args) { 
    Stack<String> stack = new Stack<>(); 
    print(5, stack); 
    while (!stack.isEmpty()) { 
     System.out.println(stack.pop()); 
    } 
}