/*
Even Fibonacci numbers
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.
题目翻译:
斐波那契数列中的每一项被定义为前两项之和。
从 1 和 2 开始,斐波那契数列的前十项为:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
考虑斐波那契数列中数值不超过 4 百万的项,找出这些项中值为偶数的项之和。
*/
#include <bits/stdc++.h>
using namespace std;
long long ans; // 记录答案
long long a = 1, b = 2, c = a + b, tmp;
int main() {
while (c <= 4000000) {
if (c % 2 == 0) ans += c;
c = a + b; a = b; b = c;
}
cout << ans << endl;
return 0;
}
输出为4613730 |