java 杨辉三角

给出如下程序模板:
import java.util.Scanner;
public class PrintYh{
public static void main(String args[]){
int Line_num;
int row,col;
int yh[]=new int[20]; //不超过20行
Scanner reader=new Scanner(System.in);
System.out.print("请输入行数:");
Line_num=reader.nextInt();
yh[0]=1;
for (【代码1】){ //控制输出行数为Line_num的循环控制
for (col=row;col>0;col--)
yh[col]=yh[col-1]+yh[col];
for (【代码2】) //控制输出每行前导空格的循环控制
System.out.print(" ");
for (col=0;col<=row;col++)
【代码3】 //输出下标为col的项的代码
System.out.println(" " );
}
}
}
要求完成各需补充代码,并调试通过。

打印杨辉三角代码如下:

public class woo {

public static void triangle(int n) {

int[][] array = new int[n][n];//三角形数组

for(int i=0;i<array.length;i++){

for(int j=0;j<=i;j++){

if(j==0||j==i){

array[i][j]=1;

}else{

array[i][j] = array[i-1][j-1]+array[i-1][j];

}

System.out.print(array[i][j]+"\t");

}

System.out.println();

}

}

public static void main(String args[]) {

triangle(9);

}

}

扩展资料

杨辉三角起源于中国,在欧洲这个表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年。它把二项式系数图形化,把组合数内在的一些代数性质直观地从图形中体现出来,是一种离散型的数与形的优美结合。

杨辉三角具有以下性质:

1、最外层的数字始终是1;

2、第二层是自然数列;

3、第三层是三角数列;

4、角数列相邻数字相加可得方数数列。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-23
import java.util.Scanner;

public class Test {
public static void main(String args[]) {
int Line_num;
int row, col;
int yh[] = new int[20]; // 不超过20行
Scanner reader = new Scanner(System.in);
System.out.print("请输入行数:");
Line_num = reader.nextInt();
yh[0] = 1;
for (row = 0; row < Line_num; row++) { // 控制输出行数为Line_num的循环控制
for (col = row; col > 0; col--){
yh[col] = yh[col - 1] + yh[col];
}
for (int i=1;i<Line_num-row;i++){ // 控制输出每行前导空格的循环控制
System.out.print(" ");
}
for (col = 0; col <= row; col++) {
System.out.print(yh[col]+" "); //输出下标为col的项的代码
}

System.out.println("");
}
}

}

 

我的上一楼不符合楼主需求,我是符合需求的第一个,希望楼主能采纳!

第2个回答  2014-06-23
public class YangHui {

public static void main(String args[]){
try{
int n = 10;
int mat[][] = new int[n][];
int i, j;
for (i = 0; i < n; i++) {
mat[i] = new int[i + 1];
//mat[i][0] = 1;
mat[i][i] = 1;
for (j = 1; j < i; j++) {
mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
}

}
for (i = 0; i < mat.length; i++) {
for (j = 0; j < n - i; j++)
System.out.print(" ");
for (j = 0; j < mat[i].length; j++)
System.out.print(" " + mat[i][j]);
System.out.println();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
求采纳为满意回答。
相似回答