看题如下: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.   

    int pre_i = 1,i = 2,sum = 3;
    while sum < 4000000 
     begin
       i += pre_i ;
       pre_i = i - pre_i;
       sum += i;
     end;
      

  2.   

    int pre_i = 1,i = 2,sum = 3;
    while sum <= 4000000 
     begin
       i += pre_i ;
       pre_i = i - pre_i;
       if ((sum + i) > 4000000) break;
       sum += i;
       
     end;
      

  3.   

    现在才发现,彻底地晕了,C++/Delphi都分不清楚了.int pre_i = 1,i = 2,sum = 3; 
    while (1) 
    {
      i += pre_i ; 
      pre_i = i - pre_i; 
      if ((sum + i) > 4000000) break; 
      sum += i; 
      

  4.   

    应该是 1,1,2,3,5,8,。。
    所以序数是三倍数的位置上都是偶数
    下面用循环就可以了int Fn_2=1;
    int Fn_1=1;
    int Fn=F1+F2;
    int sum=0;
    bool True=1;
    while(True)
    {
    if(sum<=4000000)
    break;
    else
    sum+=Fn;
    Fn_2=Fn+Fn_1;
    Fn_1=Fn-2+Fn;
    Fn=Fn_1+Fn_2;}
    cout<<sum<<endl;
      

  5.   

    clear
    clcFibonacci(1)=1;
    Fibonacci(2)=2;
    sum=1;
    i=3;while 1    
    Fibonacci(i)=Fibonacci(i-1)+Fibonacci(i-2);if mod(Fibonacci(i),2)~=0
    if sum+Fibonacci(i)>=4000000
        break;
    else
        sum=sum+Fibonacci(i);
    end
    end
    i=i+1;
    end