Recursive to Iterative Pascal's Triangle

陌路散爱 提交于 2020-01-11 13:39:10

问题


I wonder how to convert a recursive function/class to an iterative one. I have made a recursive Pascal's triangle, and now need to compare it to an iterative.

public class RecursivePascal extends ErrorPascal implements Pascal
{
    private int n;

    RecursivePascal(int n) throws Exception
    {
        super(n);
        this.n = n;
    }

    public void printPascal()
    {
        printPascal(n, false);
    }

    public void printPascal(boolean upsideDown)
    {
        printPascal(n, upsideDown);
    }

    private void printPascal(int n, boolean upsideDown)
    {
        if (n == 0) { return; }

        if (!upsideDown) { printPascal(n - 1, upsideDown); }

        for (int i = 0; i < n; i++)
        {
            System.out.print(binom(n - 1, i) + (n == i + 1 ? "\n" : " "));
        }

        if (upsideDown) { printPascal(n - 1, upsideDown); }
    }

    public int binom(int n, int k)
    {
        if (k == 0 || n == k) { return 1; }

        return binom(n - 1, k - 1) + binom(n - 1, k);
    }
}

What do I need to change in order to make it iterative? I'm still a little unsure of how this works. Thanks in advance!


回答1:


Plug these two functions below into a pascal class. I tested it and it works. That link that Prateek Darmwal posted is pretty much the same thing.

  public void nonRecursivePrint()
  {
    nonRecursivePrint(n, true);
  }

  public void nonRecursivePrint(int n, boolean upsideDown)
  {
    if (!upsideDown)
    {
      for (int j=0; j<(n+1); j++)
      {
        for (int i=0; i<(j); i++)
        {
          System.out.print(binom(j - 1, i) + (j == i + 1 ? "\n" : " "));
        }
      }
    }
    else
    {
      for (int j=n; j>0; j--)
      {
        for (int i=0; i<(j); i++)
        {
        System.out.print(binom(j - 1, i) + (j == i + 1 ? "\n" : " "));
        }
      }
    }       
  }



回答2:


Go through this link you will find your answer explained in detail http://www.geeksforgeeks.org/pascal-triangle/




回答3:


Here's the code for recursive pascal triangle,

import java.util.Scanner;

public class PascalsTriangleRecursion 
{
   public static void printPascal(int num)
   {
      for(int a = 0; a < num; a++)
      {
         for(int b = 0; b <= a; b++)
         {
            System.out.print(pascalTriangle(a, b) + " ");
         }
         System.out.println();
      }
   }

   public static int pascalTriangle(int a, int b)
   {
      if(b == 0)
      {
         return 1;
      }
      else if(b == a)
      {
         return 1;
      }
      else
      {
         return pascalTriangle(a - 1, b - 1) + pascalTriangle(a - 1, b);
      }
   }

   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter number of rows : ");
      int number = sc.nextInt();
      printPascal(number);
      sc.close();
   }
}

For more info on pascal triangle refer this resource.



来源:https://stackoverflow.com/questions/40399426/recursive-to-iterative-pascals-triangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!