斐波那契数列中的每个新项都是通过添加前两项来生成的。从 1 和 2 开始,前 10 个术语将是:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
求序列中所有不超过四百万的偶数项之和。
Int64[] Numeros = new Int64[4000005];
Numeros[0] = 1;
Numeros[1] = 2;
Int64 Indice = 2;
Int64 Acumulador = 2;
for (int i = 0; i < 4000000; i++)
{
Numeros[Indice] = Numeros[Indice - 2] + Numeros[Indice - 1];
if (Numeros[Indice] % 2 == 0)
{
if ((Numeros[Indice] + Acumulador) > 4000000)
{
break;
}
else
{
Acumulador += Numeros[Indice];
}
}
Indice++;
}
Console.WriteLine(Acumulador);
Console.ReadLine();
我的程序没有按我猜的那样运行,因为在 Project Euler 上他们说我的答案不正确。也许我忽略了一些东西。有什么帮助吗?