Uncle Lei

life is a game for everyone and love is a price.

导航

杨辉三角

Posted on 2016-12-25 13:19  chunzai  阅读(142)  评论(0编辑  收藏  举报
 1 /**
 2  * 杨辉三角
 3  * @author chunzai
 4  *
 5  */
 6 public class Test1 {
 7     public static void main(String[] args) {
 8         int a[][] = new int[7][];
 9         for(int i=0;i<7;i++){
10             a[i] = new int[i+1];
11             for(int j=i;j<6;j++){
12                 System.out.print(" ");
13             }
14             for(int k=0;k<=i;k++){
15                 
16                 if(k==0||k==i){
17                     a[i][k] = 1;
18                     
19                 }else{
20                     a[i][k] = a[i-1][k]+a[i-1][k-1];
21                     
22                 }
23                 System.out.print(a[i][k]+" ");
24             }
25             System.out.println();
26         }
27     }
28 }