杨辉三角,是二项式系数在三角形中的一种几何排列。在欧洲,这个表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年,比贾宪迟600年。杨辉三角是中国古代数学的杰出研究成果之一,它把二项式系数图形化,把组合数内在的一些代数性质直观地从图形中体现出来,是一种离散型的数与形的结合。
输出杨辉三角
public class Demo1 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("请输入你想要打印的行数:");
int num=s.nextInt()+1;
int [][] a=new int[num][2*num];
a[0][num]=1;//定义第一行
for(int i=1;i<num-1;i++){
for(int j=1;j<2*num-1;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j+1];
}
}//计算杨辉三角值
for(int m=0;m<num;m++){
for(int n=0;n<2*num;n++){
if(a[m][n]==0){
System.out.print(" ");
}//若为0,则输出空格
else{
System.out.printf("%4d",a[m][n]);
}//不为0,输出该数字
}
System.out.println();//换行
}
}
}
效果展示
请输入你想要打印的行数:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1