手写代码之写斐波那契数列-传入key值求对应的value
2024-10-06 06:50:23
public class Testany { public static void main(String[] args) {
int value = number(10);
System.out.println(value);
} public static int number(int k){
int value = 0;
int a = 1;
int b = 1;
for(int i =1; i<=k;i++){
value= a+b;
a = b;
b= value;
}
return value; }
}
public static void main(String[] args) {
int value = Fun(10);
System.out.println(value);
} public static int Fun(int k){
int arr[] = new int[k];
arr[0] = arr[1] = 1;
for (int i = 2; i < arr.length; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
return arr[k-1];
}
最简单写法
public static void main(String[] args) {
System.out.println(getFibo(10));
} private static int getFibo(int i) {
if (i == 1 || i == 2)
return 1;
else
return getFibo(i - 1) + getFibo(i - 2);
}
最新文章
- bash 语法使用
- 丹佛机场行李系统Postmortem
- CSS实现兼容性的渐变背景(gradient)效果
- coreseek实战(三):全文搜索在php中应用(使用api接口)
- [LintCode] Implement Trie 实现字典树
- ligerui ligerTip气泡提示信息
- 【MySQL】MySQL的group_concat使用例子
- [LeetCode#12] Roman to Integer
- Linq无聊练习系列4--join练习
- lesson - 11 课程笔记
- Linux 常用命令 (common commands for linux)
- luogu1345 奶牛的电信 (最小割)
- 基于URL的权限管理(三)
- es6proxy
- 一个讲课截屏 清明DAY2
- RabbitMQ fanout类型的Exchange
- 102/107. Binary Tree Level Order Traversal/II
- 雷林鹏分享:Ruby 循环
- Spring Boot(一):初步认识
- BAPC 2014 Preliminary
热门文章
- [lodop]css样式after、before添加content内容之前和之后
- 前端研究CSS之文字与特殊符号元素结合的浏览器兼容性总结
- 最简单的策略模式代替if-else实战
- 【C/C++开发】malloc,calloc和realloc的区别和注意事项
- beyond Compare 30天过期后的处理办法
- PHP使用glob方法遍历文件夹下所有文件
- [转帖]一文尽懂 USB4
- java当中JDBC当中的transaction例子
- 共阳极RGB LED二极管
- python 之 面向对象基础(继承与派生,经典类与新式类)