很有味道的算法
下面是题目,有兴趣的给结果(最好写上代码),对了一定给分.
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.
[解决办法]
UP
[解决办法]
#include <iostream.h>
void main ()
{
double array[1000] ;
double sum = 2 ;
array[0] = 1 ;
array[1] = 2 ;
for(int i=2; i<1000; i++)
{
array[i]=array[i-2]+array[i-1] ;
if((i-1)%3 == 0)
sum = sum + array[i] ;
}
cout<<sum<<endl;
}
结果3.51652e+208