java--杨辉三角


package test111111; public class Test11 { public static void main(String[] args){ printYanghui(); } //先总结一般规律,再讨论特殊情况,再做图形调整 public static void printYanghui() { int rowMax = 6; int[][] arr = new int[rowMax][rowMax]; for(int row=0;row<rowMax;row++) { for(int k=0;k<rowMax-row;k++) { System.out.print(" "); } for(int col=0;col<=row;col++) { if(col==row||col==0||row==0) { //特殊情况 arr[row][col] = 1; }else { arr[row][col] = arr[row-1][col-1]+arr[row-1][col]; //一般规律 } System.out.print(arr[row][col]+" "); } System.out.println(""); } for(int row=0;row<rowMax;row++) { for(int col=0;col<rowMax;col++) { System.out.print(arr[row][col]+" "); } System.out.println(""); } } }

    1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
 1 5 10 10 5 1

posted @ 2017-09-07 23:42  cuiz_book  阅读(122)  评论(0编辑  收藏  举报