生成帕斯卡三角形的最佳方法(提到的两种方法)

发布于 2024-11-27 13:32:08 字数 323 浏览 2 评论 0原文

我有一个 Java 编程作业。

我通过制作一个 nCr 来实现它( http://en.wikipedia.org/wiki/Combination ) 函数然后使用双 for 循环通过打印出来来制作三角形。

但是,该作业要求创建一个不均匀的二维数组,然后通过将前几行中的两个数字相加来填充,然后打印该数组。

我知道我必须按照要求的方式完成作业,但是我有一种小小的感觉(至少对于小三角形来说)我所实现的方法更好。

哪种方法更好?

I have an programming assignment in Java.

I have implemented it by making an nCr ( http://en.wikipedia.org/wiki/Combination ) function then using a double for loop to make the triangle by printing it out.

However, the assignment calls for a uneven 2d array to be created and then populated by adding the two numbers from the previous lines and then printing the array out.

I know I am going to have to do the assignment the way it asks, however I have a small feeling that (at least for small triangles anyway) that the approach I have implemented is better.

Which is the better approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

度的依靠╰つ 2024-12-04 13:32:08

我认为作业所要求的方法会更好。您的方法需要多次乘法来计算三角形的每个元素。对于需要计算的三角形的每一行,该数字都会增加。

然而,赋值方法需要对三角形的每个元素进行一次加法。

I'd think the method called for by the assignment would be better. You're method requires a number of multiplications to calculate each of the elements of the triangle. This number will increase for each row of the triangle you need to calculate.

The assignment's method, however, requires a single addition for each element of the triangle.

握住我的手 2024-12-04 13:32:08

如果我理解您的问题,那么您正在尝试比较两种生成帕斯卡三角形的方法:

  1. 通过运行 nCr 函数来填充三角形的每个单元格。
  2. 通过简单的加法填充每个单元格,一次性生成三角形。

第二种方法似乎更好。我错过了什么吗?即使您在 nCr 函数中使用记忆功能,这些调用也会产生开销。

If I understand your question, you are trying to compare two approaches to generating Pascal's triangle:

  1. By running an nCr function to populate each cell of the triangle.
  2. By generating the triangle in one pass by filling in each cell by a simple addition.

The second approach seems hands-down better. Am I missing something? Even if you use memoization in your nCr function there is overhead in those calls.

最冷一天 2024-12-04 13:32:08
  1. 使用递归

    /*通过使用递归*/
    递归帕斯卡类 {
        公共静态无效主(字符串参数[]){
            整数 n = 100;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j <= i; j++) {
                    //System.out.print(i+","+j+" ");
                    System.out.print(pascal(i, j) + " ");
                }
                System.out.println();
            }
        }
    
        静态 int pascal(int i, int j) {
            如果(j==0)
                返回1;
            否则如果 (j == i)
                返回1;
            别的 {
                返回帕斯卡(i - 1,j - 1)+帕斯卡(i - 1,j);
            }
        }
    }
    
  2. 通过使用简单的逻辑

    /*通过使用逻辑*/
    类 p {
        公共静态无效主(字符串参数[]){
            int one[] = {1};
            整数 n = 13;
            System.out.println("1");
            for (int j = 0; j < n; j++) {
                int Two[] = new int[one.length + 1];
                int 两个计数器 = 0;
                for (int i = 0; i < one.length; i++) {
                    如果(我==0){
                        二[twoCounter++] = 一[i];
                        System.out.print(one[i] + " ");
                    }
                    如果(我!= 0){
                        二[twoCounter++] = 一[i] + 一[i - 1];
                        System.out.print((one[i] + one[i - 1]) + " ");
                    }
                    if (i == one.length - 1) {
                        二[twoCounter++] = 一[i];
                        System.out.print(one[i] + " ");
                    }
                }
                System.out.println();
                一=二;
            }
        }
    }
    
  1. By using recursion

    /*By using recursion*/
    class RecursivePascal {
        public static void main(String args[]) {
            int n = 100;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j <= i; j++) {
                    //System.out.print(i+","+j+" ");
                    System.out.print(pascal(i, j) + " ");
                }
                System.out.println();
            }
        }
    
        static int pascal(int i, int j) {
            if (j == 0)
                return 1;
            else if (j == i)
                return 1;
            else {
                return pascal(i - 1, j - 1) + pascal(i - 1, j);
            }
        }
    }
    
  2. By using simple logic

    /*By using logic*/
    class p {
        public static void main(String args[]) {
            int one[] = {1};
            int n = 13;
            System.out.println("1");
            for (int j = 0; j < n; j++) {
                int two[] = new int[one.length + 1];
                int twoCounter = 0;
                for (int i = 0; i < one.length; i++) {
                    if (i == 0) {
                        two[twoCounter++] = one[i];
                        System.out.print(one[i] + " ");
                    }
                    if (i != 0) {
                        two[twoCounter++] = one[i] + one[i - 1];
                        System.out.print((one[i] + one[i - 1]) + " ");
                    }
                    if (i == one.length - 1) {
                        two[twoCounter++] = one[i];
                        System.out.print(one[i] + " ");
                    }
                }
                System.out.println();
                one = two;
            }
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文