mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 07:23:28 +08:00
36 lines
504 B
Go
Executable File
36 lines
504 B
Go
Executable File
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
|
|
}
|