看题如下: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.
解决方案 »
- 求一些公司vc笔试题目
- 如何去掉创建对话框时,系统自带的快捷键
- 请推荐一个工具,完成从latex文件到pdf的转化
- 如何在 .rc 资源文件中添加双引号。在线等候
- 如何用矩阵向量表示图形
- 请教,在一个对话框中使用picture控件属性为rectangle,如何在picture中绘制函数图像比如y=2*x*x
- 在更新数据库时,提示"不能更新;当前正在被服务器上的XXXXX的用户admin锁定",急急急!
- 如何清空Windows消息队列
- 各位大家好,我是浙江的CSDN人员为了团结各位同志我建议........?????
- 用Visual C++开发数据库,什么技术最好?
- 关于 MFC CObject 类 拷贝问题
- dll 通信
while sum < 4000000
begin
i += pre_i ;
pre_i = i - pre_i;
sum += i;
end;
while sum <= 4000000
begin
i += pre_i ;
pre_i = i - pre_i;
if ((sum + i) > 4000000) break;
sum += i;
end;
while (1)
{
i += pre_i ;
pre_i = i - pre_i;
if ((sum + i) > 4000000) break;
sum += i;
}
所以序数是三倍数的位置上都是偶数
下面用循环就可以了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;
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