算法 - 打印1000以内的斐波那契数列

栏目: 编程工具 · 发布时间: 7年前

内容简介:规律: 前两项的和跟第三项相等原链接文:写博客不易,转载请保留原文链接,谢谢!

规律: 前两项的和跟第三项相等

原链接文: https://tomoya92.github.io/2019/04/27/algorithm-2/

@Test
public void test() {
  print(1, 1, 1000);
}

/**
  * 递归打印斐波那契数列
  *
  * @param i   第一项
  * @param j   第二项
  * @param max 最大数
  */
private void print(int i, int j, int max) {
  // 如果j为1, 说明是前两项, 因为前两项都是1, 提前打印出来
  if (j == 1) System.out.print(i + ", " + j + ", ");
  int sum = i + j;
  if (sum > max) return;
  System.out.print(sum + ", ");
  print(j, sum, max);
}
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

写博客不易,转载请保留原文链接,谢谢!

原文链接:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Implementing Responsive Design

Implementing Responsive Design

Tim Kadlec / New Riders / 2012-7-31 / GBP 27.99

New devices and platforms emerge daily. Browsers iterate at a remarkable pace. Faced with this volatile landscape we can either struggle for control or we can embrace the inherent flexibility of the w......一起来看看 《Implementing Responsive Design》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具