package zsy.com;
import java.util.ArrayList;
import java.util.Scanner;
public class Yanghui {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> first = new ArrayList<Integer>();
first.add(1);
ArrayList<Integer> n = new ArrayList<Integer>();
System.out.println(" Please input the row you will get:");
Scanner in = new Scanner(System.in);
int num = in.nextInt();
System.out.println("Below is the whole Yanghui triangle:");
System.out.println(first.get(0));
getNrow(first,n,num);
System.out.println("Below is The "+num+"th row :");
for(int i=0;i<first.size();i++){
System.out.print(first.get(i)+" ");
}
}
public static void getNrow(ArrayList<Integer> have_known,ArrayList<Integer> will_know,int n){
for(int i=0;i<n;i++){
getNextrow(have_known,will_know);
have_known.clear();
for(int s=0;s<will_know.size();s++){
have_known.add(will_know.get(s));
}
will_know.clear();
System.out.println("");
}
}
public static void getNextrow(ArrayList<Integer> have_known,ArrayList<Integer> will_know){
int l = have_known.size();
for(int i=0;i<=l;i++){
if(i==0 || i==l){
will_know.add(1);
System.out.print(will_know.get(i)+" ");
}else{
int k = have_known.get(i) + have_known.get(i-1);
will_know.add(k);
System.out.print(will_know.get(i)+" ");
}
}
}
}
程序运行事例及结果: