[color=blue][/color][color=darkblue][/color]
除两侧元素均为1外,其余每个位置上的元素值其正上方元素与左上方元素之和,用数组来描述则为a[i][j] = a[i - 1][j - 1] + a[i - 1][j]
public class TextTriangle {
public static void yanghui(int a[][], int ROW){
for(int i = 0; i <= ROW; i++){
for(int j = 0; j <= a[i].length - 1; j++){
if(i == 0 || j == 0 || j == a[i].length - 1)
a[i][j] = 1;
else
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for(int i = 0; i <= ROW; i++){
for(int j = 0; j <= a[i].length - 1; j++)
System.out.print(a[i][j] + "");
System.out.println();
}
}
public static void main(String args[]){
final int ROW = 5;
int a[][] = new int[ROW + 1][];
for(int i = 0; i <= ROW; i++){
a[i] = new int[i + 1];
}
yanghui(a, ROW);
}
}
:arrow:
除两侧元素均为1外,其余每个位置上的元素值其正上方元素与左上方元素之和,用数组来描述则为a[i][j] = a[i - 1][j - 1] + a[i - 1][j]
public class TextTriangle {
public static void yanghui(int a[][], int ROW){
for(int i = 0; i <= ROW; i++){
for(int j = 0; j <= a[i].length - 1; j++){
if(i == 0 || j == 0 || j == a[i].length - 1)
a[i][j] = 1;
else
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for(int i = 0; i <= ROW; i++){
for(int j = 0; j <= a[i].length - 1; j++)
System.out.print(a[i][j] + "");
System.out.println();
}
}
public static void main(String args[]){
final int ROW = 5;
int a[][] = new int[ROW + 1][];
for(int i = 0; i <= ROW; i++){
a[i] = new int[i + 1];
}
yanghui(a, ROW);
}
}
:arrow: