题解 | #杨辉三角#
杨辉三角
https://www.nowcoder.com/practice/e671c6a913d448318a49be87850adbcc
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[][] arr = new int[n][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[i + 1]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || i == j) { arr[i][j] = 1; } else { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } System.out.printf("%5d", arr[i][j]); } System.out.println(); } } }
Java基础练习题 文章被收录于专栏
都是一些基础的语法题目,每天可以刷几道。