简体   繁体   English

仅使用递归打印金字塔

[英]Print pyramid using recursion only

I managed to print a pyramid with loops like this: 我设法用如下循环打印金字塔:

void printtree() {
  for (int i=0; i<row; i++){
    for (int j=0; j<row-i-1; j++)
      System.out.print(" ");
    for (int k=row; k>row-i-1; k--)
      System.out.print("* ");
    System.out.println();
  }
}

The output looks like this: 输出看起来像这样:

        * 
       * * 
      * * * 
     * * * * 
    * * * * * 
   * * * * * * 
  * * * * * * * 
 * * * * * * * * 
* * * * * * * * * 

I would like to make this using recursion only, how should I construct the function? 我只想使用递归来实现,应该如何构造函数? My concern is with the nested loop, I could only interprete the single inside or ouside loop. 我关心的是嵌套循环,我只能解释单个内部循环或ouside循环。 I know it is imterpreted with (row-1) spaces followed by row # of "* " on each line, then change to a new line. 我知道它用(row-1)空格解释,其后每行上都带有“ *”行号,然后更改为新行。 What is the base case here? 这里的基本情况是什么?

Since you wanted solution to use recursion only. 由于您希望解决方案仅使用递归。 I assume you don't want any loops in the program. 我假设您不需要程序中的任何循环。 If you want to convert your program quickly into recursive approach, look at each for loop and think how you can convert it. 如果要将程序快速转换为递归方法,请查看每个for循环并考虑如何将其转换。

You can convert 您可以转换

 for (int j=0; j<row-i-1; j++)
      System.out.print(" ");

to function 发挥作用

static void printSpace(int j, int i) {
        if (j < row - i - 1) {
            System.out.print(" ");
            printSpace(j + 1, i);
        }
    }

Notice that above recursive function does same as your for loop. 请注意,上述递归函数与for循环相同。 It requires same parameters which you have used in your for loop. 它需要使用与for循环相同的参数。 Now this trick you can apply to for loop which prints star as well. 现在,您可以将这个技巧应用到也可以打印星号的for循环中。

static void printStar(int k, int i) {
        if (k > row - i - 1) {
            System.out.print("* ");
            printStar(k - 1, i);
        }
    }

The similar trick can be applied to outer for loop and you can transform the code. 可以将类似的技巧应用于外部for循环,并且可以转换代码。

Complete code 完整的代码

static int row = 5;

    static void printtree() {
        printPyramid(0);
    }

    static void printPyramid(int i) {
        printSpace(0, i);
        printStar(row, i);
        System.out.println();

        if (++i < row)
            printPyramid(i);
    }

    static void printSpace(int j, int i) {
        if (j < row - i - 1) {
            System.out.print(" ");
            printSpace(j + 1, i);
        }
    }

    static void printStar(int k, int i) {
        if (k > row - i - 1) {
            System.out.print("* ");
            printStar(k - 1, i);
        }
    } 
static void recursion(int row, int k, int j)
{
if(row>10)
{
    return;
}
else
{
    if(k==row)
    {
    System.out.println();
    recursion(++row, 0,0);
    }
    else if(10-j>row)
    {
    System.out.print(" ");
    ++j;
    recursion(row, k, j);
    }
    else
    {

    System.out.print("* ");
    recursion(row, ++k, j);
    }   
}
}

call method 调用方法

recursion(0,0,0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM