1. 程式人生 > >Go語言學習和應用

Go語言學習和應用

(1)楊輝三角形

package main

import "fmt"

//行數
const LINES int = 10

// 楊輝三角
func ShowYangHuiTriangle() {
	nums := []int{}
	for i := 0; i < LINES; i++ {
		//補空白
		for j := 1; j < (LINES - i); j++ {
			//fmt.Print(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()
}

輸出結果:

package main

import "fmt"

//行數
const LINES int = 10

// 楊輝三角
func ShowYangHuiTriangle() {
	nums := []int{}
	for i := 0; i < LINES; i++ {
		//補空白
		for j := 1; j < (LINES - i); j++ {
			//fmt.Print(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()
}

 

(2)乘法表

package main

import (
	"fmt"
	"strconv"
)


func multiplicationTable()  {
	for i := 1; i <= 9; i++{
		for j := 1; j <= i; j++ {
			var ret string
			if i*j < 10 && j != 1 {
				ret = " " + strconv.Itoa(i*j)
			} else {
				ret = strconv.Itoa(i*j)
			}
			fmt.Print(j, "*", i, "=", ret, "  ")
		}
		fmt.Print("\n")
	}

}

func main()  {
	multiplicationTable()

}

輸出結果:

1*1=1  
1*2=2  2*2= 4  
1*3=3  2*3= 6  3*3= 9  
1*4=4  2*4= 8  3*4=12  4*4=16  
1*5=5  2*5=10  3*5=15  4*5=20  5*5=25  
1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  

 

(3) 使用陣列列印楊輝三角

根據上一行的內容,來獲取下一行的內容並打印出來。

package main

import "fmt"

func GetYangHui(n []int) []int{
	//fmt.Print(n)
	var n_len int
	n_len = len(n)
	m := [] int{}
	m = append(m, 1)
	for i := 1; i<n_len; i++ {
		m = append(m, n[i-1] + n[i])
	}
	m = append(m, 1)
	return m
}


func main(){
	nums := []int{}
	var i int
	for i = 0; i < 10; i++ {
		if i == 0 || i == 1 {
			nums = append(nums, 1)
			fmt.Print(nums)
			fmt.Print("\n")
		}else{
			nums = GetYangHui(nums)
			fmt.Print(nums)
			fmt.Print("\n")
		}

	}
}

方法二

package main
import "fmt"
func GetYangHuiTriangleNextLine(inArr []int) []int {
	var out []int
	var i int
	arrLen := len(inArr)
	out = append(out, 1)
	if 0 == arrLen {
		return out
	}
	for i = 0; i < arrLen-1; i++ {
		out = append(out, inArr[i]+inArr[i+1])
	}
	out = append(out, 1)
	return out
}
func main() {
	nums := []int{}
	var i int
	for i = 0; i < 10; i++ {
		nums = GetYangHuiTriangleNextLine(nums)
		fmt.Println(nums)
	}
}

輸出結果 :

[1]
[1 1]
[1 2 1]
[1 3 3 1]
[1 4 6 4 1]
[1 5 10 10 5 1]
[1 6 15 20 15 6 1]
[1 7 21 35 35 21 7 1]
[1 8 28 56 70 56 28 8 1]
[1 9 36 84 126 126 84 36 9 1]

 

(4)階乘

package main

import "fmt"

func factorial(n int) int{
	var r int
	if n>0 {
		r = n*factorial(n-1)
		return r
	}
	return 1

}

func main(){
	var n int = 10
	fmt.Print(factorial(n))
}

(5) 斐波那契數列

package main

import "fmt"

func fibonacci(n int) int {
	if n < 2 {
		return n
	}
	return fibonacci(n-2) + fibonacci(n-1)
}

func main() {
	var i int
	for i = 0; i < 10; i++ {
		fmt.Printf("%d\t", fibonacci(i))
	}
}