mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 17:27:39 +08:00
update book code
This commit is contained in:
33
eBook/exercises/chapter_9/fibo/fibonacci.go
Executable file
33
eBook/exercises/chapter_9/fibo/fibonacci.go
Executable file
@@ -0,0 +1,33 @@
|
||||
package fibo
|
||||
|
||||
/*
|
||||
func Fibonacci(n int) (res int) {
|
||||
if n <= 1 {
|
||||
res = 1
|
||||
} else {
|
||||
res = Fibonacci(n-1) + Fibonacci(n-2)
|
||||
}
|
||||
return
|
||||
}
|
||||
*/
|
||||
// accepts a general operation op:
|
||||
func Fibonacci(op string, n int) (res int) {
|
||||
if n <= 1 {
|
||||
switch op {
|
||||
case "+":
|
||||
res = 1
|
||||
case "*":
|
||||
res = 2
|
||||
default: res = 0
|
||||
}
|
||||
} else {
|
||||
switch op {
|
||||
case "+":
|
||||
res = Fibonacci(op, n-1) + Fibonacci(op, n-2)
|
||||
case "*":
|
||||
res = Fibonacci(op, n-1) * Fibonacci(op, n-2)
|
||||
default: res = 0
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user