不要追求绝对的公平,红尘之中没有公平而言,人活一世,难得糊涂。                                           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
4kDc(|+OU8K RX)_0#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:51Testing软件测试网Q:u\ U[+j
#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...51Testing软件测试网\f%e*s#|%b;Q"vD(S
#Find the sum of all the even-valued terms in the sequence which do not exceed four million.51Testing软件测试网@x3n)zU-Po
51Testing软件测试网_(~U*vU f2~
=begin51Testing软件测试网9{/? Tu6xS
max    =400000051Testing软件测试网&p{-gx#q:V+B#~J
total  = 051Testing软件测试网#[0y3^{!a o(Z
$stack = []
6BAqK'FCu0 51Testing软件测试网pt\3M7m9|q.yf
def fib n51Testing软件测试网/s [B.t u qk
  if n == 0
kGW2o4Cf @.E0    return 151Testing软件测试网JXBW+S#Y4g;F
  end
auc X4w5rYA4C0  if n == 151Testing软件测试网W&rOU]4C7U$rhW
    return 251Testing软件测试网Ovm K#dZ
  end
4@'eGt q#m{0  return $stack[n - 1] + $stack[n - 2]
ZN/L ox!]0LT)U0end51Testing软件测试网)z's.as%e/~
 
$l8p)| b%e0max.times do |i|51Testing软件测试网Fks^ f5j {L
  $stack[i] = fib i51Testing软件测试网n?vy$oyz\
  if $stack[i] > max51Testing软件测试网~6Mj"i2i.XSA
    break51Testing软件测试网&p7E%D#T(R6@
  end51Testing软件测试网k2?3x#s$s H2K
  if $stack[i] & 1 == 0
]f!a+u'Y0    total += $stack[i]51Testing软件测试网sq4@Xy%N3Mx
  end
7ZG:y$y,TNK0end
(`0]#u)\R0puts total
TlW _|wX.k0=end51Testing软件测试网}x:x#KG4Y

)M"m1d"?4tq0

TAG:

 

评分:0

我来说两句

Open Toolbar