杨辉三角

来源:互联网 发布:英雄无敌 mac 10.10 编辑:程序博客网 时间:2024/06/06 11:44
package java_test;public class YanghuiTriangle {    static void YHTriangle(){        int[][] arr=new int[10][10];        for(int row=0;row<arr.length;row++){            for(int col=0;col<=row;col++)            {                if(col==0)//第一列                    arr[row][col]=1;                else{                    arr[row][col]=arr[row-1][col]+arr[row-1][col-1];                }            }        }        for(int row=0;row<arr.length;row++){            for(int col=0;col<=row;col++)            {                System.out.print(arr[row][col]);                System.out.print("  ");            }            System.out.println();//换行        }    }    public static void main(String[] args) {        // TODO Auto-generated method stub       System.out.println("杨辉三角");       YHTriangle();    }}
0 0