不要追求绝对的公平,红尘之中没有公平而言,人活一世,难得糊涂。
it is no use doing what you like, you have got to like what you do.
Problem 2
上一篇 /
下一篇 2009-09-08 10:23:21
/ 个人分类:projecteuler题目 ruby实现
# Problem 2
51Testing软件测试网d&u |j#eb0qQ#|#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:
B1yR7cm1keuc+c/g i0#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
51Testing软件测试网|D#SO;T&Z%fB/P#Find the sum of all the even-valued terms in the sequence which do not exceed four million.
7O2@0k|,m[0t&h7qf0R'Q0=begin
51Testing软件测试网7e[f,Cltmax =4000000
#jR&dQ,X;}H/a0total = 0
-Y1[(Hz/Qcq&w_:Jt0$stack = []
T.v)ZT _#xSlXM(x0 51Testing软件测试网
o)n+E4GW6L Z G&{G
Kdef fib n
"`v1j4WG5_ro0 if n == 0
51Testing软件测试网D1N eKU {
jn8m return 1
hJq}_0 end
51Testing软件测试网#}6A&_(b6czD%B'] if n == 1
sCkD4E1c
Hz0 return 2
51Testing软件测试网;xs9j/C$i(r"w end
y;F+`+xX0 return $stack[n - 1] + $stack[n - 2]
H]!F%HYf0end
51Testing软件测试网-o Dw!i"T7r!xsuq *D;|&o
T1~dQ~0max.times do |i|
)LG!N:C \(|0 $stack[i] = fib i
51Testing软件测试网 A l'Es%`o;C*| if $stack[i] > max
,]%I!|7j,Xiy*lbTh0 break
5{5lP/Pq0 end
`k{5tIX _&{Xxd0 if $stack[i] & 1 == 0
Eh%{.dh]d3UMe]{_0 total += $stack[i]
d
{L3S@K!N0 end
GwG,gp0end
!FCWZL0puts total
51Testing软件测试网*C$u1|1K/Z
t1C
M=end
-S"?:ii~$o N051Testing软件测试网;\,m
hC{@
f Y
收藏
举报
TAG: