题目描述:
打印杨辉三角
解题思路:
• 1
代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入行数:"); int row = sc.nextInt(); int[][] arr = new int[row][row]; for (int i = 0; i < row; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { arr[i][j] = 1; } else { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } System.out.print(arr[i][j] + " "); } System.out.println(); } } }