Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >帕斯卡三角形二维数组格式化打印输出

帕斯卡三角形二维数组格式化打印输出
EN

Stack Overflow用户
提问于 2012-01-20 00:16:32
回答 5查看 28.8K关注 0票数 7

我有一个小任务,我必须使用一个二维数组来产生Pascal三角形。这是我的代码,它可以工作。如果我像这样显示三角形,就会有额外的积分机会:

但是,我的间距不是这样格式化的。它只是简单地显示左侧排列的所有数字。这很难描述,但如果你运行它,你就会明白我的意思。

下面是我的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Pascal {
    public static final int ROW = 16;
    public static void main(String[] args) {
        int[][] pascal = new int[ROW + 1][];
        pascal[1] = new int[1 + 2];
        pascal[1][1] = 1;
        for (int i = 2; i <= ROW; i++) {
            pascal[i] = new int[i + 2];
            for (int j = 1; j < pascal[i].length - 1; j++) {
                pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
            }
        }
        for (int i = 1; i <= ROW; i++) {
            for (int j = 1; j < pascal[i].length - 1; j++) {
                System.out.print(pascal[i][j] + " ");
            }
            System.out.println();
        }
    }
}

如果有人能帮我找出如何在我的程序中添加正确的间距,以产生图片中所需的输出,那就太好了。我知道我需要在某个地方放一个System.out.print(" ")。我只是不知道在哪里。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-01-20 09:44:21

我在这里修改了你的代码,由于我的控制台窗口的限制,它可以很好地打印到13行大小:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.util.*;

public class Pascal {
    public static final int ROW = 12;
    private static int max = 0;

    public static void main(String[] args) {
        int[][] pascal = new int[ROW + 1][];
        pascal[1] = new int[1 + 2];
        pascal[1][1] = 1;
        for (int i = 2; i <= ROW; i++) {
            pascal[i] = new int[i + 2];
            for (int j = 1; j < pascal[i].length - 1; j++) {
                pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
                String str = Integer.toString(pascal[i][j]);
                int len = str.length();
                if (len > max)
                    max = len;
            }
        }

        for (int i = 1; i <= ROW; i++) {
            for (int k = ROW; k > i; k--)
                System.out.format("%-" + max + "s", " ");
            for (int j = 1; j < pascal[i].length - 1; j++)
                System.out.format("%-" + (max + max) + "s", pascal[i][j]);
            System.out.println();
        }
    }
}

输出:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
                                 1     
                              1     1     
                           1     2     1     
                        1     3     3     1     
                     1     4     6     4     1     
                  1     5     10    10    5     1     
               1     6     15    20    15    6     1     
            1     7     21    35    35    21    7     1     
         1     8     28    56    70    56    28    8     1     
      1     9     36    84    126   126   84    36    9     1     
   1     10    45    120   210   252   210   120   45    10    1     
1     11    55    165   330   462   462   330   165   55    11    1     
票数 7
EN

Stack Overflow用户

发布于 2012-01-20 00:24:14

您遇到了间距问题,因为您需要向某些数字添加空格,以容纳较大数字占用的空间。首先确定您计划打印的最大数字是多少(以编程方式)。然后确定该号码日志中的位数(N)。然后,您可以使用此数字为比最大数字少的数字打印空格,以使您的打印效果更好。

票数 5
EN

Stack Overflow用户

发布于 2021-02-16 12:35:32

您可以在2d数组的左上角构建一个Pascal三角形,如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1  1  1  1  1  1  1  1  1  1
 1  2  3  4  5  6  7  8  9
 1  3  6 10 15 21 28 36
 1  4 10 20 35 56 84
 1  5 15 35 70 126
 1  6 21 56 126
 1  7 28 84
 1  8 36
 1  9
 1

两个嵌套流:在行上,然后在列上。第一行和列的元素等于1,所有其他元素都是行和列中前一个元素的总和。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
int n = 10;
// an array of 'n' rows
int[][] arr = new int[n][];
// iterate over the rows of the array
IntStream.range(0, n)
        // a row of 'n-i' elements
        .peek(i -> arr[i] = new int[n - i])
        // iterate over the elements of the row
        .forEach(i -> IntStream.range(0, n - i).forEach(j -> {
            if (i == 0 || j == 0)
                // elements of the first row
                // and column are equal to one
                arr[i][j] = 1;
            else
                // all other elements are the sum of the
                // previous element in the row and column
                arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
        }));
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// formatted output
Arrays.stream(arr)
        .map(row -> Arrays.stream(row)
                // format as a two-digit number
                .mapToObj(i -> String.format("%2d", i))
                .collect(Collectors.joining(" ")))
        .forEach(System.out::println);

"%2d" -格式为两位数字,当为n=10时,

"%4d" -格式为四位数字,当为n=16时,依此类推。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8935254

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文