Go language: Print Yang Hui triangle

Source: Internet
Author: User

The style of the Yang Hui Triangle is as follows:

                         1         1 1        1 2 1       1 3 3 1      1 4 6 4 1     1 5 ten 5 1 1   6 15 20 15 6 1  

It is characterized by that the leftmost and rightmost elements of each row are 1, while the other elements are equal to the sum of the two elements on the "shoulders" above it.

Print the Yang Hui triangle using the Go language:

Package Testimport ("FMT")//number of lines const LINES int = 8//Yang hui triangle func showyanghuitriangle () {nums: = []int{}for I: = 0; i < LINES ; i++ {//Fill blank for J: = 0; J < (Lines-i); J + + {FMT. Print ("  ")}for J: = 0; J < (i + 1);  J + + {//current array length var = len (nums)//This position should generate the number var value int//each row the first element and the last element is 1, the middle element equals two elements above it and if j = = 0 | | j = = I {value = 1} else {value = Nums[length-i] + nums[length-i-1]}nums = append (nums, value) fmt. Print (Value, "")}//a newline fmt. Println ("")}}


Go language: Print Yang Hui triangle

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.