?? yanghui.java
字號:
public class Yanghui
{
public static void main(String args[])
{
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();
}
}
}
/*
程序運行結果如下:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -