Ȼ/10

Top / Ȼ / 10

βˤĤ

ž夲β Fibonacci F_0, F_1, F_2, ... ׻ƽϤץġ׽񤱤ȤΤǡϰʲΤ褦ʤΤǤä

  1. F_0 = 0, F_1 = 1 Ȥ.
  2. ưѥ᡼ n (30餤)Ϳ졤F_0, F_1, F_2,... F_n ޤǤϤΤȤ롥
  3. ץA ϡƵѤƷ׻Ȥ롥
  4. ץB ϡ롼׷׻ѤƷ׻Ȥ롥


Ƶˤץߥ

ޤϺƵѤƥץߥ󥰤򤷤Ƥߤ褦


ι¤

ȤϤޤι¤Ǥ롥äȺäƤޤ
㤨СäȤñ˹ͤȼΤ褦ˤʤ

  1. include Math
  2.  
  3. # n ܤ Fibonacci ֤ؿ
  4. def fibonacci(n)
  5.   # ϸ
  6.   return n
  7. end
  8.  
  9. # ʲץ
  10.  
  11. # ɤޤǽϤ뤫
  12. n_upper = ARGV[0].to_i
  13.  
  14. # n_upper ܤޤǤFibonacci
  15. for i in 0..n_upper do
  16.   print("F_", i," = ",fibonacci(i),"\n")
  17. end


եܥʥåؿ:Ƶ

ơ˴οδؿ fibonacci 褦
ϥեܥʥåοŪʺƵ򤽤ΤޤѤɤʤΤǴñΤ褦ˤʤ

# n ܤ Fibonacci ֤ؿ
def fibonacci(n)
  if (n == 0) then
    return 0
  elsif (n == 1) then
    return 1
  else
    return fibonacci(n-1) + fibonacci(n-2)
  end
end

ơǴưƤߤ褦30 餤ޤǤ餻Ȥ٤Ȥ褯狼롥


롼פˤץߥ


ι¤

ι¤ϡޤΤΤƱΤͤ褦


եܥʥåؿ:롼

롼ǤˤϡΤ褦ʺȤ롼פɤ

  1. Ĥͤ­ơֿ͡פȤ.
  2. ơĤͤ򹹿.

ˤϡľ˺Τʤ㤨мΤ褦ˤʤ

  1. def fibonacci(n)
  2.   # n = 0 or 1 ʤ֤
  3.   if ( (n == 0) || (n==1) )then
  4.     return n
  5.   end
  6.  
  7.   # Τ n >= 2 ξ硥
  8.   pp_n = 0
  9.   p_n  = 1
  10.  
  11.   for i in 2..n do
  12.     cur_n = pp_n + p_n
  13.     pp_n, p_n = p_n, cur_n
  14.   end
  15.  
  16.   return cur_n
  17. end

̤ο̤ͤ˥롼ǤΥץ񤯤Ȥʴˤʤ

warning.png ơΥץ򸫤ơif else Ȥ鷺˺ѤǤ뤳Ȥ˵Ťͤ⤤
return ¹ԤȤǴؿλȤߤʤΤǡλȤߤ˻ȤäƤΤǤ롥

⤦礭ȤθդǸȡ㳰(ξ n=0 or n=1 ˤ)ϴؿǾ˹Ԥif ʸνץǺѤࡤȤȤˤʤ롥
ȡ

ʣʾʬϴؿǤ٤

ȤȤˤʤ롥


®: ׻Υʤ

Υץϡɤˤǵ᤿ͤʤɤľˤʤäƤ̵̤롥
̵̤ܤढ롥ȤäƼȼΤ褦ʴ

  1. 㤨 F_10 ׻ F_0, F_1, ... F_10 ޤǵޤΤˡ F_11 Ȥˤޤ F_0, F_1, ..., F_10 ׻ʤ̵̡
  2. 㤨 F_10 ׻ǡF_4 6׻ľ̵̡

1 ϺƵǤˤ롼Ǥˤⶦ̵̤̤Ǥꡤ2 ϺƵǤΤߤˤ̵̤Ǥ롥
warning.png Ρפ̵̤狼褯ͤ褦

Ǥ줾нͤ褦

ޤ̵ 1 ˤĤƹͻ褦
ֿפȤΤ餷ơF_n F_0, F_1, ..., F_n Τ٤Ƥ˵Τ顤Ѥ褦 ĤޤꡤF_n פȤؿǤϤʤF_0, F_1, ..., F_n פȤؿˤȼˤ줬Ǥ

warning.png Τ褦ˡؿȥץδ֤ʤɤξΡ֤ȤפŬڤ߷פȤ褤ι¤ͤȤȤΰ


ι¤

˽񤤤褦ˡfibonacci ؿ餻Ф褤Τǡ㤨мΤ褦ˤʤ

  1. include Math
  2.  
  3. # ɽؿ
  4. def list_print(ary)
  5.   # ϸ
  6. end
  7.  
  8. # Fibonacci ֤ؿ
  9. def fibonacci(n)
  10.   # ϸ
  11.  
  12.   # Ȥꤢץबư褦˶äݤǤ褤Τ֤Ƥ
  13.   return []
  14. end
  15.  
  16.  
  17. # ʲץ
  18.  
  19. # ͤäơ
  20. n_upper = ARGV[0].to_i
  21.  
  22. # Fibonacci ǵ롥
  23. fb_list = fibonacci(n_upper)
  24.  
  25. # 줿̤ɽ
  26. list_print(fb_list)

warning.png ޤι¤߷פ뤳Ȥ˺ʤ褦ˡ ץΤˤ׻񤤤ƤץϥݡȤȤƼդʤ补


ƴؿμ: ɽ

ޤɽؿϤǤ롥㤨мΤ褦ˤʤ

# ɽؿ
def list_print(ary)
  for i in 0..(ary.size-1) do
    print("F_", i," = ",ary[i],"\n")
  end
end


ƴؿμ: fibonacci : 롼

ˡfibonacci ؿ
ޤϤξϴñʥ롼Ǥ󥸤褦
ȤȤΤ狼䤹񤱤ơ㤨мΤ褦ˤʤ

# Fibonacci ֤ؿ
def fibonacci(n)
  # ǽĤϴˤ狼äƤ롥
  fb_list = [0,1] 

  # Ĥ­ɲäƤ
  for i in 2..n do
    fb_list.push( fb_list[i-2] + fb_list[i-1] )
  end

  # 夬ä֤
  return fb_list
end

ǥ롼Ǥ̵̤ۤʤʤä
ΤΤ٤ơ֤®ʤäƤ롥

warning.png n 礭ʤä硤ɤ줯餤㤦ͽۤ补


ƴؿμ: fibonacci : Ƶ: ̵ 1 ӽ

ơޤ̵ 1 ӽ褦ϸȡF_n ΤޤФ褤Τǡ㤨мΤ褦ˤʤ

# Fibonacci ֤ؿ
def fibonacci(n)
  # ޤͤξ.
  if (n == 0) then
      return [0]
    elsif (n == 1) then
      return [0,1]
  end

  # ʲn >= 2 ξ.
  # ǤĤ˷׻ơ
  pp_list = fibonacci(n-2)
  p_list = fibonacci(n-1)

  # ׻ɲá
  result = pp_list[n-2] + p_list[n-1]
  fb_list = p_list.push(result)

  # ֤
  return fb_list
end

̵ 1 ӽǤȤˤʤ롥
¹ԤƤߤȤ狼뤬ΥץϤ٤
ȤΤ⡤̵ 2 ̵ 1 礭ΤǡӽƤʤΥץǤϤޤޤȤȤʤΤǤ롥


ƴؿμ: fibonacci : Ƶ: ̵ 1,2 ӽ

ơ̵ 2 ӽͤ褦

Ǿǫ˹ͤɬפ롥⤷褦
ޤ̵ 2 ϡ֤狼ˤ֤ǡסֲƱ׷׻뤳Ȥˤäȯ롥
׻ν֤狼䤹Ф˱ץ򤹤̵̤ʤǽ뤬ξϤ⤤ʤ

ǡ˷׻ν֤˰¸ʤкȤơ

ʣ׻ʤˡΰ:
η׻̤ϿƤơ
׻򤹤˷׻ѤߤǤʤå(ⲽɤ)

Ȥʤ롥


OK.png ⲽɤϡƵ˸¤餺ĤǤѤǤ롥׻̵̤ȤѶŪ˻ȤäƤߤ褦

ơξ϶ŪˤϡֳϤפ򸫤Ĥġ̤Ǥ˽񤤤ƤФ֤񤤤Ƥʤз׻ƵϿƷ̤֤ȤȤˤʤ롥
Υץȡ㤨мΤ褦ˤʤϤˤĤƤεҤɬפʤΤǡΤܤ褦

  1. # ɽؿ
  2. def list_print(ary)
  3.   for i in 0..(ary.size-1) do
  4.     print("F_", i," = ",ary[i],"\n")
  5.   end
  6. end
  7.  
  8. # Fibonacci ˤĤƤΥ֤ؿ
  9. def fibonacci(n, m_list)
  10.  
  11.   # ˥˵Ͽʤ,Τޤޥֵ.
  12.   if (m_list[n] > -1) then
  13.     return m_list
  14.   end
  15.  
  16.   # ʲϥ˵Ͽʤä硥
  17.  
  18.   # ƵǤĤ˷׻.
  19.   pp_n = fibonacci(n-2, m_list)[n-2]
  20.   p_n = fibonacci(n-1, m_list)[n-1]
  21.  
  22.   # ׻ƥ˵Ͽ
  23.   m_list[n] = pp_n + p_n
  24.  
  25.   # ֵ.
  26.   return m_list
  27. end
  28.  
  29. # ʲץ
  30.  
  31. # ͤäơ
  32. n_upper = ARGV[0].to_i
  33.  
  34. # fibonacci ؿϤ(-1 ǡ̤Ͽפ̣Ƥߤ)
  35. memo = Array.new( n_upper+1 ){ -1 }
  36. memo[0] = 0
  37. memo[1] = 1 
  38.  
  39. # Fibonacci ǵ롥ۤȤ̤ΥϤȡ֤äƤ.
  40. fb_list = fibonacci(n_upper, memo)
  41.  
  42. # 줿̤ɽ
  43. list_print(fb_list)

ơưƤߤ褦
̵̵̤ʤä櫓ǤϤʤǤ⡤ƵǤˤ⤫餺®ư뤳Ȥʬ


ʸ

ơϾӿѤʸˤĤƳؤܤ
ʸȤʸνǡΥԥ塼ǤΤ褦ʰΤȻפäɤ

ޤϰʲ˴ñʸˡɽ򼨤

ʸ ʰɽ

ʸˡ. s="aiueo kakiku keko" ȤƤ
ԡ.dup Ĥ롥ĤΥԥ塼ǤʸΥԡüʤΤաa = s.dup Ȥ a s ƱȤʸˤʤ롥 a = s Ȥʤ!
Ĺ.length Ĥs.length 17 ˤʤ
Ϣ+ Ȥs + "sa" "aiueo kakiku kekosa" ˤʤ
Ϣ<< Ȥs << "sa" s 񤭴 "aiueo kakiku kekosa" ˤʤ
ʬ.split() Ĥ. ̤.s.split() ["aiueo", "kakiku", "keko"] ˤʤ
ʸʬ.split("ʸ") Ĥ롥̤s.split("i") ["a", "ueo kak", "ku keko"] ˤʤ
nܤʸ (*1)Ʊ͡[n-1] Ĥ롥ξ̤ʸɤʤΤs[4] 111 ˤʤ(111 "o" ʸ)
ʸɤʸľ.chr Ĥs[4].chr "o" ˤʤ
iʸܤ jʸܤޤǤʬʸ (*2)[(i-1)..(j-1)] Ĥ.s[0..4] "aiueo" ˤʤ
iʸܤ nĤʸʬʸ (*3)[(i_1),n] Ĥs[0,5] "aiueo" ˤʤ
ꤷʬ֤.ꤷʬ = "ʸ" ȤǤ褤ꤷʬȿʸĹϰäɤ. (*1,2,3)٤ƤǻȤ.s[4] = "z" Ȥ s "aiuez kakiku keko" ˤʤ
.index("ʸ") ĤȡõƽƸĤä֤֤s.index("u") 2 ˤʤ
.rindex("ʸ") ĤȡõƽƸĤä֤֤s.rindex("u") 11 ˤʤ
.index("ʸ",ʸ) Ĥȡꤷʸ֤ϤơõƽƸĤä֤֤s.index("u",2) 2 ˤʤ뤬 s.index("u",3) 11 ˤʤ
.rindex("ʸ",ʸ) Ĥȡꤷʸ֤ϤơõƽƸĤä֤֤s.rindex("u",11) 11 ˤʤ뤬 s.rindex("u",10) 2 ˤʤ
andִ(սΤ).sub("֤Ťʸ", "֤ʸ") Ĥ. ǽ˸Ĥäսִs.sub("a","A") Ȥȡs "Aiueo kakiku keko" ˤʤ
andִ().gsub("֤Ťʸ", "֤ʸ") Ĥ. սִs.gsub("a","A") Ȥȡs "Aiueo kAkiku keko" ˤʤ
Ǹ˲ʸäƤк.chomp! Ĥ(ä)
ʤɤʸѴ.to_s Ĥ1234.to_s "1234" ˤʤ


Ƥߤ褦: 3Ĥ

ޤϴñǴ褦

notes.png (Ȥꤢ) 3ܿ or 3ĤˤĤƤΤüɽԤؿꡤȤäƵưͿ(30 餤)ޤǤδؿɽƤߤ褦


 ruby -w test.rb 30

ȤƼ¹Ԥȡ

1

2

!!!!

4

5

!!!!

7

8

!!!!

10

11

!!!!

!!!!

14

!!!!

16

17

!!!!

19

20

!!!!

22

!!!!

!!!!

25

26

!!!!

28

29

!!!!

Ȥ褦ʷ̤Ǥ褦ˤ衤ȤȤˤʤ롥

warning.png (Ը) index Ǹʸ󤬸ĤäˤʤꡤĤʤ nil ȤüʤΤˤʤ롥
ơ

Ruby nil false ֵפȤư
ʳϡֿפȤư

Τǡindex 򤽤Τޤ if ʸʤɤȽʬ˻ȤȤǤ롥
ФȤƤϡs = gets while ξ˻ȤΤˤ褯Ƥ롥


դοǿ?

Ҥä֤Ƥǿˤʤ褦ǿõȤͤ롥

Ūˤϡ㤨 113 ǿǡҤä֤ 311 ϤǿǤ롥
(ʾǤʤỌ̇̄ʤ)

notes.png ()11ʾꤷʲϰϤ鸫Ĥ󤹤ץ


Ūˤϡ㤨

 ruby -w inv.rb 800

ȤƼ¹Ԥȡ

(11, 11) are prime numbers.

(13, 31) are prime numbers.

(17, 71) are prime numbers.

(31, 13) are prime numbers.

(37, 73) are prime numbers.

(71, 17) are prime numbers.

(73, 37) are prime numbers.

(79, 97) are prime numbers.

(97, 79) are prime numbers.

(101, 101) are prime numbers.

(107, 701) are prime numbers.

(113, 311) are prime numbers.

(131, 131) are prime numbers.

(149, 941) are prime numbers.

(151, 151) are prime numbers.

(157, 751) are prime numbers.

(167, 761) are prime numbers.

(179, 971) are prime numbers.

(181, 181) are prime numbers.

(191, 191) are prime numbers.

(199, 991) are prime numbers.

(311, 113) are prime numbers.

(313, 313) are prime numbers.

(337, 733) are prime numbers.

(347, 743) are prime numbers.

(353, 353) are prime numbers.

(359, 953) are prime numbers.

(373, 373) are prime numbers.

(383, 383) are prime numbers.

(389, 983) are prime numbers.

(701, 107) are prime numbers.

(709, 907) are prime numbers.

(727, 727) are prime numbers.

(733, 337) are prime numbers.

(739, 937) are prime numbers.

(743, 347) are prime numbers.

(751, 157) are prime numbers.

(757, 757) are prime numbers.

(761, 167) are prime numbers.

(769, 967) are prime numbers.

(787, 787) are prime numbers.

(797, 797) are prime numbers.

Ȥ褦ʽϤ򤹤ץ񤳤ȤȤˤʤ롥

warning.png ʸνդޤˤ̿⤢뤱ɤ⡤Τ˼ʬǽ񤤤Ƥߤ褦


ž夲

ʲ䤳? ʤΤޤᡤĤʬ褦


ƥν

ĤΡַƱ׿ n, m Ȥ褦
ơn 1ΰ̤ο a_1, 10 ΰ̤ο a_2, 100 ΰ̤ο a_3, ... m 1ΰ̤ο b_1, 10 ΰ̤ο b_2, 100 ΰ̤ο b_3, ... Ȥ
ΤȤʲΤ褦ʾץ񤳤

  1. n, m ϥץεư˥ѥ᡼ȤͿ롥
  2. X Y Ȥ򼡤Τ褦롥
    1. ⤷ a_i = b_i ʤ X_i = 1, Ǥʤ X_i = 0. X_i = 1 Ȥ a_i, b_i ϰʹߤĴоݤȤʤ
    2. ˡa_i = b_i ǤϤʤb_i Ʊ a_j ʤ Y_i = 1, Ǥʤ Y_i = 0. Y_i = 1 Ȥ a_j, b_i ϰʹߤĴоݤȤʤ
    3. X = X_i ιסY= Y_i ι
  3. n, m Фơ X, Y ׻Ϥؿ xy(n,m) ץˤ롥
  4. xy(n,m) ȤäƵ᤿ X, Y ɽ롥

㤨С

 ruby -w xy.rb 12345 54321

ȤƼ¹Ԥȡ

X: 1, Y: 4

ȤϤ֤褦ˤȤȤˤʤ롥
(¯˸ Hit and Blow ȸƤФƥΥ롼Ǥ롥褯ΤʤͤϥͥåȥĴ٤Ƥߤ褦)


ƥ

ΡֽפѤƿƥ
n ץǺ餻ơʹ֤ϤФ X, Y Ϥ뤳ȤǥҥȤͿƤޤϤ򷫤֤褦ˤ褦
ΤȤϰʲ̤ꡥ

  1. ֲ n ˤ뤫פηϥץεư˥ѥ᡼ȤͿ롥
  2. n 롥
  3. ʹ֤Ϥ gets Ǽ롥
  4. Ϥ X, Y ϤƤϤԤĤ褦ˤ롥
  5. n Ʊʹ֤Ϥ顤λݤɽƽλ롥

warning.png ư˥ѥ᡼Ϳȡgets Ϥե̾ȤߤʤƤɤ߹⤦Ȥ롥 ϤϺΤǡñ gets ȤΤǤϤʤ$stdin.gets ȤޥɤȤƻȤ

㤨СƥȤΤ n Ϥ褦ˤƤƤΥץ

 ruby -w hb.rb 2

ʤɤȤƵưȡΤ褦ˤȤǤ뤳Ȥˤʤ롥

Ans: 29

m? : 21

X: 1, Y: 0

m? : 12

X: 0, Y: 1

m? : 19

X: 1, Y: 0

m? : 91

X: 0, Y: 1

m? : 92

X: 0, Y: 2

m? : 29

Conguratulaions!


(Ը) ƥ: ԥ塼ĩǤ

ΥΥ롼ǡʹ֤Фƥԥ塼Ƥ褦ȤƤȤץ


ݡ

ֵӹԤä½ˤĤơñˤޤȤ᤿򤻤.
ޤž夲ʬǼʬǽ񤤤ץ𤻤.

Ƽ

  1. °(ز)
  2. ֹ
  3. ǯ
  4. ̾
  5. οΥݡ(θȤˤĤƵŤ)

񤯤Τ˺ʤ褦.

about Icons, ClipArts

Some icons in this page are downloadable at ICONFINDER.

The "note" icon notes.png designed by Marco Martin is distributed with the LGPL licence,
the "warning" icon warning.png designed by Alexandre Moore with the GPL licence
and the "triangle" icon JNorth_arrow-right-sm.png designed by Joseph North is distributed with the Creative Commons (Attribution-Noncommercial-Share Alike 3.0 Unported) licence.

Some clip arts used in this page are downloadable at Open Clip Art Library.
We deeply appreciate their superb works. With licence, they describe that "the actual clipart content on open clipart library is Public domain" in the web.