Created
January 21, 2020 08:22
-
-
Save Jason-cqtan/477851723c556b44e141c74bef057b56 to your computer and use it in GitHub Desktop.
斐波那契数列go版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func main() { | |
//返回10个斐波那契数列,todo 使用int,结果数字会越界 | |
fibonacciIndex(10) | |
} | |
func fibonacciIndex(length int) { | |
slice := make([]int,length) | |
for i:=0;i<len(slice) ;i++ { | |
tem := 0 | |
if i < 1 { | |
tem = 0 | |
} else if i < 2 { | |
var sum = (i - 1) + (i - 2) | |
tem = int(math.Abs(float64(sum))) | |
slice[i] = tem | |
}else { | |
tem = slice[i-1] + slice[i-2] | |
slice[i] = tem | |
} | |
} | |
fmt.Println(slice) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment