golang闭包,传统斐波那契
2024-09-03 09:48:35
package main
import (
"fmt"
)
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Printf("%d:%d\n", i, f())
}
}
// func fibonaci(n int) (ret int) {
// if n <= 1 {
// ret = 1
// } else {
// ret = fibonaci(n-1) + fibonaci(n-2)
// }
// return ret
// }
func fibonacci() func() int {
f0, f1, f2 := 1, 1, 0
index := -1
return func() int {
index++
if index == 0 {
return 1
} else if index == 1 {
return 1
} else {
f2 = f0 + f1
f0 = f1
f1 = f2
return f2
}
}
}
最新文章
- Maven 整合FreeMarker使用
- 让你的JS更优雅的小技巧
- Ubuntu 汉化时ubuntu software database is broken错误解决
- 开发中model,entity和pojo的区别
- iOS开发之网络编程--5、NSURLSessionUploadTask+NSURLSessionDataDelegate代理上传
- ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor.error:34 - Developer Notification
- winform中嵌入Ppt、Word、Excel
- Lucene/ElasticSearch 学习系列 (2) Information Retrival 初步之名词解释
- IOS离线教程下载与Dash的使用
- hdu 2147 kiki&#39;s game 博弈论
- MySQL开启远程链接(2014.12.12)
- 能发送http请求(get,post)的工具
- c# 服务程序重启自身
- JS的一些常见验证代码
- A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)
- Error Code: 1068. Multiple primary key defined
- thinkphp框架的大D方法应用
- Docker打包 Asp.Net Core应用,在CentOS上运行(转)
- ifconfig和ping
- ACM-ICPC 2018 沈阳赛区网络预赛 K题
热门文章
- hint: not have locally. This is usually caused by another repository pushing
- 北京Python开发培训怎么选?
- 关于static关键字的思考
- Attention-based Model
- cogs——49. 跳马问题
- python爬虫20 | 小帅b教你如何使用python识别图片验证码
- 手机浏览PC版页面出现背景图片显示不全的问题解决方案
- (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
- ACDream - Xor pairs
- CODEVS——T 1049 棋盘染色