博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
斐波那契数列
阅读量:3956 次
发布时间:2019-05-24

本文共 838 字,大约阅读时间需要 2 分钟。

2021-05-10

学习视频:

 什么是斐波那契数列:

斐波那契数列是递归的典型应用;

①,前两项数除外,都是1;

②,从第三项起,每一个数都是它前两个数的和;

比如:1 1 2 3 5 8 13 21 ...

 

代码:

package per.zhangyh.algorithm.data_structure.code;/** *@author:zhangyonghui; *@date: 2021/5/10; 7:50 *@Describe: 递归与斐波那契数列 */public class FeiBoNaQie {    /*      什么是斐波那契数列:        ①,前两项数除外,都是1;        ②,从第三项起,每一个数都是它前两个数的和;        比如:1 1 2 3 5 8 13 21 ...    */    public static void main(String[] args) {        System.out.println("斐波那契数列 = " + getFeiBoNaQieNum(6));    }    /**     * todo 返回指定位置的斐波那契数     * @param index:指定的位置     */    public static int getFeiBoNaQieNum(int index) {        if (index == 1 || index == 2) { //如果是第一个数和第二个数            return 1;        }else{ //从第三项起,每一个数都是它前两个数的和            int num = (getFeiBoNaQieNum(index - 1) + getFeiBoNaQieNum(index - 2)); //这里就用到了递归            return num;        }    }}

 

转载地址:http://lytzi.baihongyu.com/

你可能感兴趣的文章
hud——1465不容易系列之一(错排问题)
查看>>
hud——2037今年暑假不AC(贪心算法)
查看>>
hustoj——练习赛i题
查看>>
hdu——1686Oulipo(kmp)
查看>>
hdu——1556Color the ball(树状数组)
查看>>
hdu——1541Stars(树状数组)
查看>>
快速幂的精简代码
查看>>
求大数乘方的前n位数字(对数加快速幂)
查看>>
hdu——2602Bone Collector(第一类背包问题)
查看>>
hdu——1711Number Sequence(kmp专练)
查看>>
hdu——2087剪花布条 ( kmp 和 find用法 )
查看>>
strstr函数和find函数的异同
查看>>
hdu——3746Cyclic Nacklace(kmp专练)
查看>>
hdu——1358Period(kmp专练)
查看>>
hust——1010F - The Minimum Length(kmp专练)
查看>>
poj——2406Power Strings(kmp专练)
查看>>
poj——2752Seek the Name, Seek the Fame(kmp专练 找出前后相同的字串)
查看>>
校赛 选修课网址 1096: Is The Same?(kmp或者find)
查看>>
选修课网址 1088: The Owl and the Fox
查看>>
校赛 选修课网址 1097: Meeting
查看>>