甚至1000岁以下的斐波那契数的总和
Sum of even Fibonacci numbers under 1000
#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;
}
我在终端上获得了极大的价值。我不知道怎么了。请帮助。
您正在计算到第1000个斐波那契号的总和不超过1000。删除 c
并测试您所产生的斐波那契号是否小于1000,而不是测试是否测试是否是否测试C小于1000。
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;
}
我想这是家庭作业吗?小心,这是初学者的高级代码。
#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;
}
我是C 的初学者,我已经完成了Euler项目的第一个问题,我使用了一种非常简单的方法来解决它,这是我的代码
#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;
}
/**
* 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));
}
}
相关文章:
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- 虚假唤醒是否会解锁所有等待线程,甚至是不相关的线程?
- 最烦人的解析甚至更令人烦恼
- 如何在C++中写入 1000 个文件时有效地缓冲
- 甚至在开始执行之前出现分段错误
- 未初始化的局部变量错误甚至认为我初始化了它(C++)
- 为什么 Lisp 中 1000 阶乘的计算如此之快(并显示正确的结果)?
- 为什么 std::find( s.begin(), s.end(), val ) 比集合 s 的 s.find(val) 慢 1000 倍<int>?
- 工作线程一直在等待,condition_variable甚至调用了notify_all
- 为什么可以将 std::unique_ptr* u1 <A>作为 u1[1000] 访问并且它仍然有效
- 为什么我们甚至需要删除运算符?(我们不能只使用删除[]吗)
- 为什么添加析构函数(甚至是空的)会破坏我的结构,该结构使用 ref 转发和折叠来保存 ref 或值的副本?
- C++:创建1000次唯一的随机分布,在任何分布中都没有重复的数字
- int x=1000和just x=1000产生不同的值
- C++2骰子滚动1000万次开始
- 电脑从1-1000猜你的号码(必须在10次尝试中)
- 是否有任何版本的C++(甚至是试行标准)是"throw()"并不意味着"cannot throw, ever"?
- 掷2个骰子1000次
- boost::mpl 不使用(甚至不兼容)std::p air 的原因是什么?
- 甚至1000岁以下的斐波那契数的总和