dabocaiqq 2020-07-09 11:32 采纳率: 54.8%
浏览 116
已采纳

高分悬赏:Java语言怎么输出如下的三角形

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

  • 写回答

4条回答 默认 最新

  • 毕小宝 博客专家认证 2020-07-18 09:33
    关注

    杨辉三角最本质的特征是,它的两条斜边都是由数字1组成的,而其余的数则是等于它肩上的两个数之和。

    package algorithm;
    
    
    public class YFTriangle {
          public static void main(String[] args) {
              yh(19);
            }
          /**
           * 它的两条斜边都是由数字1组成的,而其余的数则是等于它肩上的两个数之和。
           * @param in
           */
            public static void yh(int in){
                int[] a = new int[in + 1];
                int previous = 1;
                for (int i = 1; i <= in; i ++){
                    for (int j = 1; j <= i; j++){
                        int current = a[j];
                        a[j] = previous + current;
                        previous = current;
                        System.out.print(a[j] + " ");
                    }
                    System.out.println();
                }
            }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?