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