Java Problem Summary 17-Yang Hui Triangle implementation:
Code:
Package Java3;import java.util.scanner;/** * @author xubo601450868 * @time 20150827 * @version v1.0 * Yang Hui triangle * Enter an arbitrary positive number greater than 0 N,n is the number of layers or rows of the Yang Hui Triangle, outputting the N-layer Yang Hui triangle * */public class Test2 {public static void Main (string[] args) { Scanner s=new Scanner ( system.in); System.out.println ("Please enter the number of rows in the Yang Hui triangle:"); int n=s.nextint ();; Int[][] Y=new int[n][]; for (int i=0;i<n;i++) { y[i]=new int[i+1]; } /* Test *///for (int i=0;i<y.length;i++) {//for (int j=0;j<y[i].length;j++) {// System.out.print (y I [j]+ "\ T"),// }// System.out.print ("\ n"),// } System.out.println (n+ "layer of the Yang Hui Triangle is:"); for (int i=0;i<y.length;i++) { y[i][0]=1; Y[i][y[i].length-1]=1; for (int j=0;j<y[i].length;j++) { if (j>0) && (j<y[i].length-1)) { y[i][j]=y[i-1][j-1]+y[ I-1][J]; } System.out.print (y[i][j]+ "\ t"); } System.out.print ("\ n");}}
The output is:
Please enter the number of rows in the Yang Hui triangle: 1010-storey Yang Hui triangle: 11112113311464115101051161520156117213535217118285670562881193684126126843691
Analysis: The test is N 10 of the Yang Hui Triangle, the other can be analogy.
Since the maximum value of the int data can be 2147483647 , overflow occurs when n equals 35, and can be improved with long data.
The maximum number of layers for the int type Yang Hui triangle is 34.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The implementation of the 17-Yang Hui triangle in the Java problem summary