- package com.baobaotao.test;
-
- public class YanghuiTriangle {
- public static void main(String[] args) {
- printTriangle(7) ;
- }
-
- /**
- *
- * @param rowNum 行数
- */
- public static void printTriangle(int rowNum) {
- int triangle[][] = new int[rowNum][] ;
- for(int i=0;i<triangle.length;i++) {
- triangle[i] = new int[i+1] ;
- for(int k=0;k<rowNum-i-1;k++) {
- System.out.print(" ");
- }
- for(int j=0;j<=i;j++) {
- if(i==0 || j==i || j==0) {
- triangle[i][j] = 1 ;
- }else {
- triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j] ;
- }
- System.out.print(triangle[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
输出:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1