不要追求绝对的公平,红尘之中没有公平而言,人活一世,难得糊涂。                                           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 251Testing软件测试网2C+iIy`j
#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:
*W}/Gx4mL6Y9_w0#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...51Testing软件测试网-L`"v)izZ)p-l]
#Find the sum of all the even-valued terms in the sequence which do not exceed four million.51Testing软件测试网a c\nU*Mjg/_;j
51Testing软件测试网S2S'b7SpKY g+R
=begin
B$YIrcY^ v0max    =400000051Testing软件测试网2DI Y y%rv
total  = 0
i2K?QT.v0$stack = []51Testing软件测试网nTO#mvsqyW|
 51Testing软件测试网&N]d:{:Tr
def fib n51Testing软件测试网8{/t9Z6t0f*xV
  if n == 0
#RWL$AfB1gt0    return 151Testing软件测试网g)lL|HG-N4yc
  end51Testing软件测试网5F4K6Q+|H;e
  if n == 151Testing软件测试网M7zo3w7DN,Y!IA
    return 2
\S%Mc1?8L%]X)s Lf0  end51Testing软件测试网T,_ H&H(J6v
  return $stack[n - 1] + $stack[n - 2]51Testing软件测试网3rBt4\C/Q a3_
end
5~ c~Zi ?.C7UeQm0 51Testing软件测试网 y+E~C#J2R'y w
max.times do |i|
r ])w,I$w9sL$`x0  $stack[i] = fib i51Testing软件测试网^"h C-G*j/t6p}#Ua
  if $stack[i] > max
q^g#z U*\0    break51Testing软件测试网:L*X2[:v H}
  end
!]]$Q ` }-g4v i0  if $stack[i] & 1 == 0
"E g-Z,hG*vJ0    total += $stack[i]
yx&l X4e0  end51Testing软件测试网1i.g#cc T|;tjO4o
end
7X;F.FUup0puts total 51Testing软件测试网!|"M)g Cv]
=end
o5UE+s E9f051Testing软件测试网9C DZ7ce*st8\kN2W#_0e

TAG:

 

评分:0

我来说两句

Open Toolbar