No2 Java

来源:互联网 发布:app直播系统源码转让 编辑:程序博客网 时间:2024/04/30 00:01

题目:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

 

翻译:


斐波那契数列的每一项,都是前两项的和。从1和2开始。找出所有不超过四百万的偶数项的和。

 

解题思路:

 

直接展开此数列,然后找出所有偶数项,再全加一下,问题可以被解决。不过非常之不优美。

 

假设如下:

A, b,c, D, e ,f, G 此数列为斐波那契片段,大写的字母是偶数,他们是间隔出现的,至于为什么是中间隔两个奇数,那你得自己拍拍小脑瓜。

那么 e=D+c = D+D-b,  f=D+e=D+D+c=2D+A+b ==> G=e+f=4D+A

 

有了这个公式,得到结果的代码就简洁多了。

 

代码:

原创粉丝点击