C++ 求斐波那契序列中偶数值项的和(欧拉投影)

C++ 求斐波那契序列中偶数值项的和(欧拉投影),c++,fibonacci,C++,Fibonacci,我正在尝试从Project Euler.net解决问题。我遇到的问题是这样的 通过考虑Fibonacci序列中值不超过400万的项,求偶数值项之和。 我已经想出了以下代码 #include<iostream> #include<cstdlib> using namespace std; int main() { int a=1,b=1,c,sum=0; while(c<4000000) { c=a+b; if((c%

我正在尝试从Project Euler.net解决问题。我遇到的问题是这样的

通过考虑Fibonacci序列中值不超过400万的项,求偶数值项之和。 我已经想出了以下代码

#include<iostream>
#include<cstdlib>
using namespace std;

int main() {
int a=1,b=1,c,sum=0;
    while(c<4000000)
    {
        c=a+b;
        if((c%2)==0)
        sum+=c;

        a=b;
        b=c;
    }
     cout<<sum;
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int a=1,b=1,c,和=0;

虽然(c在进入循环之前,您没有初始化
c
。如果它包含的内容大于您的限制,循环将不会执行,
sum
将在循环终止后保持
0

在进入循环之前,您没有初始化
c
。如果它包含的内容大于您的限制,则循环将停止不执行,循环终止后,
sum
将保持
0

公共类示例{

public static void main(final String args[]) {


    // fibno me even terms
    int a = 0, b = 1;
    int temp = 0;

    // print fibo below 4 million
    for (int i = 0; i <= 4000000; i++) {

        int c = a + b;

        // System.out.println(c);

        if (c % 2 == 0)

        {

            temp = temp + c;


            // System.out.println("Even" + c);
        }
        a = b;
        b = c;

    }

    System.out.println("temp value is " + temp);

}
publicstaticvoidmain(最终字符串参数[]){
//不,我连个词都没有
INTA=0,b=1;
内部温度=0;
//打印低于400万的fibo
对于(int i=0;i公共类样本{

public static void main(final String args[]) {


    // fibno me even terms
    int a = 0, b = 1;
    int temp = 0;

    // print fibo below 4 million
    for (int i = 0; i <= 4000000; i++) {

        int c = a + b;

        // System.out.println(c);

        if (c % 2 == 0)

        {

            temp = temp + c;


            // System.out.println("Even" + c);
        }
        a = b;
        b = c;

    }

    System.out.println("temp value is " + temp);

}
publicstaticvoidmain(最终字符串参数[]){
//不,我连个词都没有
INTA=0,b=1;
内部温度=0;
//打印低于400万的fibo
对于(int i=0;i