本文介绍了Pascal三角形定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个Java程序,可以打印出一个帕斯卡三角形,但是我不知道如何正确定位它.

I made a Java program that prints out a pascal triangle, however I can't figure out how to correctly position it.

程序1

public class Triangle {
    public static void main() {
        System.out.println("\nTriangle: ");
        int row = 11;
        long[][] triangle = new long[row][row];
        triangle[1][1] = 1;
        System.out.print(triangle[1][1] + "\n");

        for (int i = 2; i<row; i++) {
            for (int n = 1; n<row; n++) {
                triangle[i][n] = triangle[i-1][n-1] + triangle[i-1][n];
                if (triangle[i][n]>0) {
                    System.out.print(triangle[i][n] + " ");
                }
            }
            System.out.println();
        }
    }
}

输出:

1
1 1
1 2 1
1 3 3 1

程序2

public class Triangle {
    public static void main() {
        System.out.println("\nTriangle: ");
        int row = 11;
        long[][] triangle = new long[row][row];
        int x = 1;
        while (x<row-1) {
            System.out.print(" ");
            x++;
        }
        triangle[1][1] = 1;
        System.out.print(triangle[1][1] + "\n");

        for (int i = 2; i<row; i++) {
            x = i;
            while (x<row-1) {
                System.out.print(" ");
                x++;
            }
            for (int n = 1; n<row; n++) {
                triangle[i][n] = triangle[i-1][n-1] + triangle[i-1][n];
                if (triangle[i][n]>0) {
                    System.out.print(triangle[i][n] + " ");
                }
            }
            System.out.println();
        }
    }
}

输出:

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1
1 5 10 10 5 1 //(Notice this line is incorrectly positioned)

当三角形接近多个数字时,它开始分解并变得丑陋.有人可以解释一下如何显示普通三角形而不是这个丑陋的三角形吗?

When the triangle approaches multiple digit numbers, it starts to break down and makes it ugly. Can someone explain how I can display a normal triangle instead of this ugly one?

推荐答案

动态Pascal三角形生成器在这里:

import java.io.IOException;
import java.util.Scanner;

public class Main
{
static double fact(int n)
{
    double result = 1;
    for (double i = 1; i <= n; i++)
        result *= i;
    return result;
}

static double combine(int n, int r)
{
    return ((fact(n)) / (fact(n - r) * fact(r)));
}

static void pascalTriangle(int n)
{
    int n2 = n;
    for (int i = 0; i < n; i++)
    {
        for (int space = 8 * (n2 - 1); space >= 0; space--)
        {
            System.out.printf(" ");
        }
        for (int j = 0; j <= i; j++)
        {
            System.out.printf("%14.0f", combine(i, j));
            System.out.printf("  ");
        }
        System.out.println();
        n2--;
    }
  }

  public static void main(String[] args) throws IOException, InterruptedException
    {
    @SuppressWarnings("resource")
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter Number of Lines(n): ");
    int n = sc.nextInt();
    pascalTriangle(n);
    System.out.println("Press any key to exit! ");
    sc.nextByte();
    }
}

这篇关于Pascal三角形定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:53