java程序怎么实现直角的杨辉三角？

6个回答

...

public class k {
public static void main(String[] args) {
int row = 10;
int[][] result = new int[row][row];
for (int i = 0; i < row; i++) {
result[i][0] = 1;
System.out.print(result[i][0] + "\t");
for (int j = 1; j <= i; j++) {
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
System.out.print(result[i][j] + "\t");
}
System.out.println();
}
}
}

public class A{

public static void main(String[] args) {
int rows = 10;
for(int i =0;i<rows;i++) {
int number = 1;
for(int j=0;j<=i;j++) {
System.out.format("%4d",number);
number = number * (i - j) / (j + 1);

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

public class YanghuiTriangleExample {

public static void main(String[] args) {
int rows = 10;
for(int i =0;i<rows;i++) {
int number = 1;
//打印空格字符串
System.out.format("%"+(rows-i)*2+"s","");
for(int j=0;j<=i;j++) {
System.out.format("%4d",number);
number = number * (i - j) / (j + 1);

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

public class A
{
public static void main(String args[])
{
int x=10;

int num[][] = new int[x][x];
for(int m=0;m<x;m++){
for(int n=0;n<=m;n++){
if(n==0||m==n){

num[m][n]=1;

}

else

num[m][n]=num[m-1][n-1]+num[m-1][n];
}
}
for(int i=0;i<x;i++){
System.out.print(" ");
for(int j=x-i;j<=x;j++)
{
System.out.print(num[i][x-j]+" ");

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