【问题标题】:Sum of even Fibonacci numbers under 10001000 以下的偶数斐波那契数之和
【发布时间】:2014-01-26 07:02:49
【问题描述】:
#include <iostream>
#include <math.h>
#include <iomanip>

int main(int argc, const char * argv[])
{
    register double j = 1, i = 0, sum=0, sum2 = 0;

    std::cout.setf(std::ios::fixed);

    for (register double c=1; sum=i+j,i=j,j=sum,c<1000; ++c)
        floor(fmod(sum,2))==0?sum2+=sum:0;

    std::cout << std::setprecision(0) << sum2;

    return 0;
}

我在终端上获得了惊人的巨大价值。我不知道出了什么问题。请帮忙。

【问题讨论】:

  • 您是在尝试编写 C 还是 C++?你有一个 C 标签,但那是 C++ 代码。
  • 为什么这些变量都是double,为什么是register,为什么是?:而不是if,为什么你在循环测试中计算斐波那契数而不是body?跨度>
  • 因为您正在运行循环 1000 次。为什么你得不到巨大的价值?
  • 取决于你所说的疯狂。它确实增长得相当快。 fib(5) = 5, fib(10) = 55, fib(15) = 610, fib(20) = 6765, ...
  • 另外,您只需要在第一个偶数之后添加每个第三个 fib 数字,即 F(3)。甚至每隔 3 个位置就会出现一次小故障。

标签: c++ math fibonacci


【解决方案1】:

您正在计算第 1000 个斐波那契数的总和,而不是 1000。删除 c 并测试您生成的斐波那契数是否小于 1000,而不是测试 c 是否小于 1000。

【讨论】:

  • 啊我怎么看不到?!删除了变量 c 并添加了一个检查条件来测试循环中 sum 的值。感谢您的帮助伙伴!
【解决方案2】:
main()
{
  int n, c, first = 0, second = 1, next;

  cout << "Enter the number of terms of Fibonacci series you want" << endl;
  cin >> n;

  cout << "First " << n << " terms of Fibonacci series are :- " << endl;

  for ( c = 0 ; c < n ; c++ )
  {
     if ( c <= 1 )
     next = c;
     else
     {
      next = first + second;
      first = second;
      second = next;
     }
     cout << next << endl;
 }

return 0;
}

【讨论】:

    【解决方案3】:

    我猜这是家庭作业?请注意,对于初学者来说,这是相当高级的代码。

    #include <iostream>
    
    class FibonacciGenerator {
    public:
      FibonacciGenerator() : f0(0), f1(1) {}
      int operator()() {
        int f2 = f0 + f1;
        f0 = f1;
        f1 = f2;
        return f0;
      }
    private:
      int f0;
      int f1;
    };
    
    int main(){
      FibonacciGenerator fibgen;
      int sum = 0;
      for( int f = fibgen(); f < 1000; f = fibgen() ){
        if( f % 2 == 0 ) sum += f;
      }  
      std::cout << sum << std::endl;
    }
    

    【讨论】:

    • 酷!从来没有想过使用运算符重载来解决它。我认为该函数也是内联的,所以开销很小,对吧?不,这不是家庭作业。如果你真的想知道的话,它就是 Project Euler。
    【解决方案4】:

    我是 C++ 的初学者,我已经完成了 Project Euler 的前 2 个问题,我使用了一个非常简单的方法来解决它,这是我的代码

    #include <iostream>
    using namespace std;
    int main() {
    //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.
    
    //since it had already said the first two number has to be 1 and 2, initialize variable n1, n2 and also n3 to store the sum of n1, n2
    //and also a variable to store the even valued terms
    int n1 = 1;
    int n2 = 2;
    int n3 = 3;
    int nsum = 0;
    //now start a for loop
    for (int count = 0; count < n3; count++) {
        //since n2 will be always bigger than count, it will be a unstoppable loop
        //so we will add a break when n2 is bigger than 4 million
    
        //now start hte Fibonacci sequence
        n3 = n1 + n2;
    
        //change the value of old n1 to old n2
        n1 = n2;
    
        //also do this to n2
        n2 = n3;
    
        //now check the value of n3 to see if it's even,if it is, add it to nsum
        if (n3 % 2 == 0) {
            nsum += n3;
        }
    
        //add the break when n3 is over 4 million
        if (n3 >= 4000000) {
            break;
        }
    }
    //now display the value
    cout << nsum << endl;
    //add the pause
    system("PAUSE");
    return 0;
    

    }

    【讨论】:

      【解决方案5】:
      /**
       * Write a method that returns the sum of all even Fibonacci numbers.
       * Consider all Fibonacci numbers that are less than or equal to n.
       * Each new element in the Fibonacci sequence is generated by adding the previous two elements.
       * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
      */
      
      import java.io.*;
      public class EVENFAB
      {
          public static int EvenFabbonicSum(int n)
          {
              int previousNum=1;
              int currentNum=2;
              int evenFabSum=0;
              do
              {
                  if(currentNum%2==0)
                  {
                      evenFabSum+=currentNum;
                  }
                  int temp = currentNum+previousNum;
                  previousNum = currentNum;
                  currentNum = temp;
               
               }
               while (currentNum < n);
              return evenFabSum;
          }
          public static void main (String[] Args)
          {
              System.out.println("\u000C");
              int a = 1000;
              System.out.println(EvenFabbonicSum(a));
          }
          
      }
      

      【讨论】:

        猜你喜欢
        • 2015-07-25
        • 1970-01-01
        • 2011-02-04
        • 2016-04-07
        • 2015-11-12
        • 2021-12-21
        • 1970-01-01
        • 2011-02-16
        • 2020-01-15
        相关资源
        最近更新 更多