import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List> outerList = new ArrayList<>();
for(int i=0;i
List inner = new ArrayList<>();
for(int j=0;j<=i;j++) {
inner.add(1);
}
outerList.add(inner);
}
List InnerList = new ArrayList<>();
for(int i=2;i
InnerList = outerList.get(i);
for(int j=1;j
int num1 = outerList.get(i-1).get(j-1);
int num2 = outerList.get(i-1).get(j);
InnerList.set(j,num1+num2);
}
}
for(List list : outerList) {
for(Integer num : list) {
System.out.printf("%5d",num);
}
System.out.println();
}
}
} 第二种简便方法:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List> outerList = new ArrayList<>();
for (int i = 0; i
List inner = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
inner.add(1);
} else {
Integer integer1 = outerList.get(i - 1).get(j - 1);
Integer integer2 = outerList.get(i - 1).get(j);
inner.add(1,integer1+integer2);
}
}
outerList.add(inner);
}
for(List list : outerList) {
for(Integer num : list) {
System.out.printf("%5d",num);
}
System.out.println();
}
}
}