P5732 【深基5.习7】杨辉三角---Java

题目描述

给出 n(n≤20)n(n\le20)n(n≤20),输出杨辉三角的前 nnn 行。

如果你不知道什么是杨辉三角,可以观察样例找找规律。
输入格式

无
输出格式

无
输入输出样例
输入 #1

6

输出 #1

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
import java.util.Scanner;
public class P5732 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int top = 1;
        int[] arr = new int[n+1];
        for (int i = 1; i < n+1; i++) {
            for (int j = 1; j <= i ; j++) {
                    int temp = arr[j];
                    arr[j] = top+temp;
                    top = temp;
                System.out.print(arr[j]+" ");
            }
            System.out.println();
        }
    }
}

你可能感兴趣的:(洛谷)