不要追求绝对的公平,红尘之中没有公平而言,人活一世,难得糊涂。                                           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软件测试网'O6\:nC-qV`
#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软件测试网s-[7cO(}y Y+sI#Sr
#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...51Testing软件测试网FcsM)d+J `
#Find the sum of all the even-valued terms in the sequence which do not exceed four million.51Testing软件测试网N8E Ix7n \ @(C Y

}8LI]8aiU C0=begin
kr8I EIp,l/r0max    =400000051Testing软件测试网'|D} ^A3w-Au$l [ i(^ r
total  = 0
3\Or0Ns@p0$stack = []
xmI7^Yx0 51Testing软件测试网 Q f6` H C
def fib n51Testing软件测试网%j G$T%[x%Bf
  if n == 051Testing软件测试网DGh0F2@l&s`U
    return 1
f+dc3sE;FN,o0  end
x;A&`i8V7d&Q%^M0  if n == 151Testing软件测试网J'p{!z)R)`%Ie
    return 251Testing软件测试网2w(L{;L*{Z
  end51Testing软件测试网(F_a4iM f&CUA
  return $stack[n - 1] + $stack[n - 2]
,\ u;X1Z5~Q0K2j\0end51Testing软件测试网~Y'f,V*iMM
 
QXVG|%QP(] y[x0max.times do |i|51Testing软件测试网&^(Z+zL_ak#P
  $stack[i] = fib i51Testing软件测试网 oXubCS
  if $stack[i] > max
;e&XE5LXh)MG0B lcL0    break
%cEz/b8O?1Z0  end51Testing软件测试网L;G,T^[ XO e
  if $stack[i] & 1 == 0
U9Z3r8Y!A j0    total += $stack[i]
A*G,}y9yf0  end
{;L}z!j0end51Testing软件测试网D'{P k5}I7Kwk~;g
puts total
l.QY NR4fzak \9X@0=end
4X/}c A5k X S e0
u$B'wl ^:K.z*l0

TAG:

 

评分:0

我来说两句

Open Toolbar