1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import java.util.Scanner;
public class ArrayTest5 {
public static int num(int x, int y) { if(y==1 || y==x) return 1; int c=num(x-1,y-1)+num(x-1,y); return c; } public static void main(String[] args) { @SuppressWarnings("resource") Scanner input=new Scanner(System.in); System.out.print("输入打印杨辉三角形的行数:"); int row=input.nextInt(); int[][] yhsj=new int[row+1][row+1]; for(int i=1;i<=row;i++) { for(int j=1;j<=i;j++) { yhsj[i][j]=num(i,j); } } for(int i=1;i<=row;i++) { for(int j=1;j<=row-i;j++) { System.out.print(" "); } for(int j=1;j<=i;j++) { System.out.print(yhsj[i][j] + " "); } System.out.println(); } }
}
|