▼页尾

[Project Euler] Problem 2

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, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

#include <iostream>
using namespace std;

int main(){
int sum = 0;
int a = 1, b = 2, c = 3;
while(b < 4000000){
sum
+= b;
a
= b+c;
b
= a+c;
c
= a+b;
}
cout
<< sum << endl;
return 0;
}

求所有Fibonacc数列中值为偶数的数的和

观察数列,容易看出每隔两个数有一个偶数

所以,我们每三个数递推一次 


posted @ 2011-02-20 20:46  xiatwhu  阅读(203)  评论(0编辑  收藏  举报
▲页首
西