杨辉三角形

杨辉三角形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import "fmt"
//行数
const LINES int = 10
// 杨辉三角
func ShowYangHuiTriangle() {
nums := []int{}
for i := 0; i < LINES; i++ {
//补空白
for j := 0; j < (LINES - i); j++ {
fmt.Print(" ")
}
for j := 0; j < (i + 1); j++ {
var length = len(nums)
var value int
if j == 0 || j == i {
value = 1
} else {
value = nums[length-i] + nums[length-i-1]
}
nums = append(nums, value)
fmt.Print(value, " ")
}
fmt.Println("")
}
}
func main() {
ShowYangHuiTriangle()
}
坚持技术分享,您的支持将鼓励我继续创作!