如何打印出某些斐波那契数列? [英] How to print out this certain fibonacci sequence?

查看:85
本文介绍了如何打印出某些斐波那契数列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static int getFib(int num) {

        if (num < 2) {
            return num;
        }
        return getFib(num - 1) + getFib(num - 2);
    }

如何使用此代码来打印此示例输出,例如以相同格式打印的附件文件

How can I use this code to print out this sample output like file attached with the same format of print out

推荐答案

假设您的 getFib()是这样的:

public static int getFib(int num) {
        if (num < 2) {
            return num;
        }
        int tmp = getFib(num - 1) + getFib(num - 2);
        return tmp;
    }

main()函数中,按要求的次数调用 getFib()函数,并以以下方式打印返回的值:

In the main() function call the getFib() function for the asked number of times and print the values returned, as:

for(i=0;i<numberOfTimes;++i){
     System.out.println(getFib(i));
}

这篇关于如何打印出某些斐波那契数列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆