匿名用户
1级
2011-09-20 回答
public static void main(String[] args) {
int i, j, k = 0;
for (i = 0; i <= 7; i++) {
for(int m = 7; m > i; m--){
System.out.print(" ");
}
for (j = 1; j < 2 * k; j *= 2) {
System.out.printf("%d ", j);
}
k = j;
for (; j >= 1; j /= 2) {
System.out.printf("%d ", j);
}
System.out.printf("\n");
}
}
我写的,和你不一样的
import java.util.Scanner;
public class YangHuiSanJiao {
public static void main(String[] args) {
System.out.print("Please input the lines to genereate:");
final int n = new Scanner(System.in).nextInt();
int[][] ary = generateParscal(n);
for(int[] item: ary){
for(int value: item){
System.out.print((value > 0? value: " "));
}
System.out.println();
}
}
private static int[][] generateParscal(final int n) {
int[][] ary = new int[n][2*n-1];
for(int i = 0; i < ary.length; i++){
for(int j = 0; j < ary[i].length; j++){
if(Math.abs(j - (n-1)) == i){
ary[i][j] = 1;
}
}
try{
int position = n - i;
do{
ary[i][position]= ary[i-1][position-1] + ary[i-1][position+1];
position++;
}while(ary[i-1][position] != 1);
}catch(ArrayIndexOutOfBoundsException exp){
continue;
}
}
return ary;
}
}