The implementation of the 17-Yang Hui triangle in the Java problem summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.