HDOJ 2032 杨辉三角

简介: Problem Description 还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1Input 输入数据包含多个测试实例,每个测试实例的...

Problem Description
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Input
输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。

Output
对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。

Sample Input
2 3

Sample Output
1
1 1

1
1 1
1 2 1

import java.util.Scanner;

public class Main {
    static int[][] pt= new int[31][];
    public static void main(String[] args) {
        pt();

        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
            int n= sc.nextInt();

            topt(n);

            System.out.println();
        }

    }

    private static void topt(int n) {
        for(int i=1;i<=n;i++){
            System.out.print(pt[i][1]);
            for(int j=2;j<=i;j++){
                System.out.print(" "+ pt[i][j]);
            }
            System.out.println();

        }
    }

    private static void pt() {
        for(int i=1;i<=30;i++){
            pt[i] = new int[i+1];
            pt[i][i]=1;
            pt[i][1]=1;
        }
        for(int i=3;i<=30;i++){
            for(int j=2;j<i;j++){
                pt[i][j]=pt[i-1][j-1]+pt[i-1][j];
            }
        }

//        for(int i=1;i<=30;i++){
//            for(int j=1;j<=i;j++){
//                System.out.print(pt[i][j]+" ");
//            }
//            System.out.println();
//        }

    }
}
目录
相关文章
|
6月前
hdoj 3555 BOMB(数位dp)
hdoj 3555 BOMB(数位dp)
19 0
HDOJ 2032 杨辉三角
HDOJ 2032 杨辉三角
93 0
HDOJ 1056 HangOver(水题)
HDOJ 1056 HangOver(水题)
82 0
HDOJ 1056 HangOver(水题)
HDOJ 1058 Humble Numbers(打表过)
HDOJ 1058 Humble Numbers(打表过)
81 0
HDOJ 2089 不要62(打表)
HDOJ 2089 不要62(打表)
98 0
|
测试技术
HDOJ 1108 最小公倍数
HDOJ 1108 最小公倍数
75 0
HDOJ(HDU) 2503 a/b + c/d(最大公约数问题)
HDOJ(HDU) 2503 a/b + c/d(最大公约数问题)
111 0
HDOJ 2009 求数列的和
HDOJ 2009 求数列的和
97 0
|
算法
HDOJ/HDU 1015 Safecracker(深搜)
HDOJ/HDU 1015 Safecracker(深搜)
77 0
HDOJ/HDU 2551 竹青遍野(打表~)
HDOJ/HDU 2551 竹青遍野(打表~)
87 0

热门文章

最新文章