Fibonacci使用1个变量 [英] Fibonacci using 1 variable

查看:104
本文介绍了Fibonacci使用1个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在接受采访时被问到以下问题:

I was asked the following question in an interview:


有没有办法只使用1个变量生成Fibonacci系列?

Is there any way in which Fibonacci series can be generated using only 1 variable ?

我不知道该回答什么。我该怎么说?

I didn't know what to answer. What should I have said?

推荐答案

是的,您可以使用封闭式表达式

其中

您可以使用<$计算表达式c $ c> double 并将结果四舍五入到最接近的整数。由于浮点运算的精度有限,这个公式将给出足够大的n的错误答案,但我认为它适用于结果适合Java 32位整数的情况。

You can calculate the expression using a double and round the result to the nearest integer. Because of the finite precision of floating point arithmetic this formula will give a wrong answer for large enough n, but I think it will work in the case when the result fits into a Java 32-bit integer.

这篇关于Fibonacci使用1个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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