原题:
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.
翻译:
/*
* 翻译:
* 这是一组 斐波纳契数列 ,找到所有的偶数,并累加他们。
*
*/
解题思路:
1 /// <summary>
2 /// 解题思路:
3 /// 很遗憾,直到现在我都不理解如何可以修改下面这段代码。因为它有很多冗余的过程。
4 /// 它的冗余出现在了if (tmp % 2 == 0)这步。
5 /// 理由是,其实偶数是每三次出现一次,他是有规律的。
6 /// </summary>
2 /// 解题思路:
3 /// 很遗憾,直到现在我都不理解如何可以修改下面这段代码。因为它有很多冗余的过程。
4 /// 它的冗余出现在了if (tmp % 2 == 0)这步。
5 /// 理由是,其实偶数是每三次出现一次,他是有规律的。
6 /// </summary>
代码:
代码
1 using System;
2
3 class EvenValuedInFibonacci
4 {
5 internal static int GetTheSumOfEvenValuedInFibonacci()
6 {
7 int Fir = 1;
8 int Sec = 1;
9 int tmp = 0;
10 int Sum = 0;
11 while (tmp <= 4000000)
12 {
13 tmp = Fir + Sec;
14 if (tmp % 2 == 0)
15 {
16 Sum += tmp;
17 }
18 Fir = Sec;
19 Sec = tmp;
20 }
21
22 return Sum;
23 }
24 }
25
26 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xbxbxb007/archive/2009/09/03/4514960.aspx
2
3 class EvenValuedInFibonacci
4 {
5 internal static int GetTheSumOfEvenValuedInFibonacci()
6 {
7 int Fir = 1;
8 int Sec = 1;
9 int tmp = 0;
10 int Sum = 0;
11 while (tmp <= 4000000)
12 {
13 tmp = Fir + Sec;
14 if (tmp % 2 == 0)
15 {
16 Sum += tmp;
17 }
18 Fir = Sec;
19 Sec = tmp;
20 }
21
22 return Sum;
23 }
24 }
25
26 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xbxbxb007/archive/2009/09/03/4514960.aspx