One-dimensional array implements the Yang Hui triangle, dimension Yang Hui triangle

Source: Internet
Author: User

One-dimensional array implements the Yang Hui triangle, dimension Yang Hui triangle

Yang Hui triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 43411 Accepted Submission (s): 18254


Problem Description Do you still remember the Yang Hui triangle I learned in middle school? The specific definition is not described here. You can refer to the following figure:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
 
The Input data contains multiple test instances. The Input of each test instance only contains a positive integer n (1 <=n <= 30), indicating the number of layers in the Yang Hui triangle to be output.
Output corresponds to each input. Please Output the Yang Hui triangle of the corresponding layers. Integers of each layer are separated by a space. An empty line is added after each Yang Hui triangle.
Sample Input
2 3
 
Sample Output
11 111 11 2 1
 
Authorlcy
SourceC language programming exercises (5)


Import java. util. optional; public class Main {static int [] mat = new int [31]; public static void main (String args []) {nation SC = new Nation (System. in); mat [0] = 1; // initialize the first element, that is, the values in the first column are all 1; int n = 0; while (SC. hasNext () {n = SC. nextInt (); triangle (n) ;}} public static void triangle (int n) {System. out. println (mat [0]); mat [n-1] = 1; // forward from the back to the front for (int I = 1; I <n; I ++) {// control the number of rows mat [I] = 1; for (int j = I-1; j> 0; j --) {mat [j] = mat [j] + mat [j-1]; if (mat [j] <0) {return ;}} for (int j = 0; j <= I; j ++) {if (j = 0) {System. out. print (mat [j]);} else {System. out. print ("" + mat [j]) ;}} System. out. println ();} System. out. println ();}}


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.