Ȼ/09

Top / Ȼ / 09

βˤĤ

ޤž夲ƷǤȡʲΤ褦Ǥä


n x m ΥǡϤ顤ιžֹ򤽤äݤɽץ񤳤
warning.png ǤդΥιФưʤȤʤ
ηˤʤäƤʤǡꤷʤƤɤ

ι¤

ʤˤϤȤ⤢졤ǽι¤Ǥ롥
ξ硤žʤɤɬפʤΤǡθ㤨мΤ褦ˤʤ

  1. include Math
  2.  
  3. # ٥ȥɽ(Υץफ饳ԡ)
  4. def print_vec(v_x)
  5.   print("[ ")
  6.   for i in 1..v_x.size do
  7.     print(v_x[i-1]," ")
  8.   end
  9.   print("]\n")
  10. end
  11.  
  12. # ɽ(Υץफ饳ԡ)
  13. def print_mtx(m_a)
  14.   for i in 1..m_a.size do
  15.     print_vec(m_a[i-1])
  16.   end
  17. end
  18.  
  19. # ž֤Ԥ
  20. def transposed_mtx(m_a)
  21.   # ϸǡ
  22.   return m_a
  23. end
  24.  
  25.  
  26. # ʲץ
  27.  
  28. # Ϥʬ
  29.  
  30. # Ϲ礭ʬʤʾ塤ǽ϶äݤˤƤʤ
  31. matrix = []
  32.  
  33. # Ϥ¥
  34. print("? : ")
  35. while (x = gets) do
  36.   x.chomp!
  37.   data = x.split()
  38.   data.map!{ |e| e.to_f }
  39.   # ξޤǤϷޤäѥ.
  40.  
  41.   # ơ˥ǡưä­ࡩ
  42.  
  43.   # Ϥ¥
  44.   print("? : ")
  45. end
  46.  
  47. # Ϲɽ
  48. print("Input matrix = \n")
  49. print_mtx(matrix)
  50.  
  51. print("\n")
  52.  
  53. # Ϲž֤ɽ
  54. print("Transposed matrix = \n")
  55. print_mtx(transposed_mtx(matrix))

񤤤ƤޤȻĤäϤñʤȤʬ롥

ϥǡȤư

ޤϡϤ줿ֹԥǡפ­ʬ褦ΤϼΤ褦ˤʤ

# Ϥ¥
print("? : ")
while (x = gets) do
  x.chomp!
  data = x.split()
  data.map!{ |e| e.to_f }
  # ξޤǤϷޤäѥ.

  # ԥǡ push Ǥ褤
  matrix.push(data)

  # Ϥ¥
  print("? : ")
end

ž

Ȥžֽ⤦ȤʤäƤϴñǡΤ褦ˤʤ

def transposed_mtx(m_a)
  # ͤȡIJΥϲˡ롥
  n = m_a.size
  m = m_a[0].size

  # žֹΤȤ롥
  m_b = Array.new(m){ Array.new(n){0.0} }
  
  # ȤƤ.
  for i in 1..n do
    for j in 1..m do
      m_b[j-1][i-1] = m_a[i-1][j-1]
    end
  end

  # žֹϡ
  return m_b
end

äƤߤФä񤷤ʤ

ؿμʸ: Ƶ

ơRuby ʤɤΤٿʸǤϡʵǽִؿκƵפǽǤ롥
ǡκƵˤĤƳؤܤ

ޤѸ򤷤ƤȡؿκƵǽȤϡִؿʸˡʬȤθƤӽФȤפȤΤǤ롥
ȤäƤԥʤ顤Ȥä褦

notes.png sum(n) := 1 + 2 + ... + n ȤؿƵȤäƤߤΤǡʲ㤫򤷤褦

ޤоݤȤʤؿʬȤƤӽФǡֿŪˡʤ
sum ȡ(n Ȥ)Τ褦ˤǤ뤳Ȥޤ򤷤褦
sum.png
warning.png դ뼰Ǥ뱦դˡդƱ sum ؿо줷Ƥ뤳Ȥܤ٤

ƵΩľȤ?

κƵְ̣ġפˤϰʲΥݥȤƤ뤳ȤɬפǤ롥褯ͤƤߤ褦

  1. դդ֤˱դ֤ ³ƤˤʤäƤ롥
  2. ִη֤ɬפ褦ˤʤäƤȡɬ n=1 ξã褦ˤʤäƤ롥

NG.png ִηֽ֤ʤԤä硤ץϱʱߤʤ! ɬǽŪ˴뤹褦դ褦

ơƵΰ̣Ϥ狼äȤơץѤˤϤɤ뤫ؤܤ
¤ϻȤϴñǡ֤ۤܤΤޤޡ׽񤱤ɤ
㤨Фξϥץ񤯤ȼΤ褦ʴˤʤ롥

def sum(n)
  if (n == 1) then
      return 1
    else
      return sum(n-1) + n
  end
end


ơưΤ? Ȥij⤤Ǽºݤ˻Ƥߤ褦
κƵȤäơsum(10) ʤɤΤˤʤ뤳ȤǧƤߤ褦

ơƵΤʤˤʤΤ򼨤ԤˤϤ⤦狼ȻפƵκϤĤΤΤǤ롥

OK.pngƵϡ񤯤Ǥ褤.
׻ץषʤɤ.

褯ͤԻ׵ĤʤȤǤϤʤ֤ŬѤФĤɬ뤹ʤΤ顤ԥ塼ƤʤΤ
줬ºݤϤȤƤʤȤ¿Τʲ򤷤褦

桼åɤθ߽ˡ

Ĥ n, m (n >= m) κͤ褦
λŪ˼Ωġ

n m κ m (n mod m) κ

äơȤĤ n, m (n >= m) κؿ gcd(n,m) ˤĤƼΤ褦ʺƵΩġ
gcd.png
warning.png μƵξƤ뤳Ȥǧ褦.

򤽤Τޤޥץߥ󥰤ȰʲΤ褦ˤʤ롥(n >= m) ȤǰΤ˳μ¤ˤƤΤˤäȤɬפɤäƤ뤫ϲΥץɤ⤦

def gcd(n,m)
  # ǰΤᡤn >= m μ¤ˤƤ.
  if (n < m) then
    n,m = m,n
  end

  # ȤϺƵμ̤.
  if (n % m == 0) then
      return m
    else
      return gcd(m, (n % m) )
  end

end

notes.png Ǽºݤ㤨 120 45 κ 15 ޤ뤳Ȥǧ褦
warning.png κƵֻȤ鷺ˡ׺ץबɤ줯餤ݤʤΤˤʤ뤫褦.


ʤηв᤬褯ʬʤȤͤϡΤ褦вϤ褦˿դäץưƤߤɤ

  1. include Math
  2.  
  3. # ؿ
  4. def gcd(n,m)
  5.   # ǰΤᡤn >= m μ¤ˤƤ.
  6.   if (n < m) then
  7.     n,m = m,n
  8.   end
  9.  
  10.   #  оݤȤƤĤοȡ mod .
  11.   print(n," mod ",m, " = ",n % m,"\n")
  12.  
  13.   # ȤϺƵμ̤.
  14.   if (n % m == 0) then
  15.       # ׻λ
  16.       print("Finished. \n")
  17.       return m
  18.     else
  19.       return gcd(m, (n % m) )
  20.   end
  21. end
  22.  
  23. # ʲץ
  24. n = 1071
  25. m = 1029
  26.  
  27. # ؿȤäƷ׻ɽ
  28. print("gcd(",n,", ",m,") = ",gcd(n,m),"\n")

롼Ȥδط

for while ʤɤΥ롼פǤϥ롼פäƤƤӥ롼פưưϼʬȤƤӽФƵܼŪˤƱ԰٤
ȤȤϡ롼׷׻ϺƵǽ񤭤ʤȤǤϤդޤΤϤ
Ĥޤꡤ

롼׷׻ϺƵǽľȤ衤
Ƶϥ롼׷׻ǽľȤϤ

ȤȤˤʤ롥

롼פƵǽľ

㤨С Fizz Buzz ؿǿѴ줿åץƵȤäƽ񤭴ȰʲΤ褦ˤʤ롥

  1. include Math
  2.  
  3. # Fizz Buzz ؿ
  4. def fb(n)
  5.   if (n % 15 == 0) then
  6.       return "Fizz Buzz"
  7.     elsif (n % 3 == 0) then
  8.       return "Fizz"
  9.     elsif (n % 5 == 0) then
  10.       return "Buzz"
  11.     else 
  12.       return n
  13.   end
  14. end
  15.  
  16. # Ѵ줿 last_n ˤʤޤĴ³ؿ
  17. # i Fizz Buzz ؿǤ줫å
  18. # n ޤѴ줿θĿ
  19. def fb_count(i, n, last_n)
  20.  
  21.   # ޤѴפ­ʤʤ
  22.   if (n < last_n) then 
  23.     # ̽
  24.     print(fb(i),"\n")
  25.  
  26.     #Fizz Buzz νϤñʤǤʤ = Ѵ줿פȤƥȤ䤹
  27.     if (fb(i) != i) then
  28.       n += 1
  29.     end
  30.  
  31.     # ο
  32.     i += 1
  33.  
  34.     # ʬȤˤ
  35.     fb_count(i, n, last_n)
  36.  
  37.   end
  38. end
  39.  
  40. # ʲץ
  41.  
  42. # Ѵ줿򤤤Ŀ뤫
  43. n_upper = ARGV[0].to_i
  44.  
  45. # Ѵ줿 n_upper ˤʤޤǥå.
  46. fb_count(1,0,n_upper)

warning.png while 롼פ˺ƵȤƤʬäܤơ򤷤褦


Ƶ롼פǽľ

㤨кǽκƵǤ sum ؿ⡤for 롼פǽľΤϴñ
ޤ, 桼åɤθ߽ˡƱͤŬڤʥ롼פǽ񤭴ǽǤ롥

notes.png (֤;͵Τ͸) 桼åɤθ߽ˡƵȤ鷺˥ץߥ󥰤Ƥߤ褦


Ƶȥ롼סɤä?

Ƶȥ롼׷׻ܼŪƱʤ顤ɤ֤٤Ȥ꤬ǤƤ롥
ˤĤƤϡĤλ롥

ޤץΤ狼䤹ˤ롥桼åɤθ߽ˡʤɤϺƵ狼䤹 Fizz Buzz ʤɤϥ롼פ狼䤹
礭񤤤Ƥȡ

Ƶȥ롼פΤɤ餬狼䤹ˤ

ˡ׻®䥳ԥ塼ˤ٤ˤĤƤǤ롥
ϤۤܤϤäꤷƤơʬȤ򲿲ƤӽФȤ߾Ƶ٤⤤
礭񤤤Ƥȡ

ƵˡʤΤ MP (MP = Machine Power)

Ȥʤ롥
ɡåȤǥåȤ⤢ΤǡƵȤ٤ɤϥХȤȤˤʤ롥


ž夲

Fibonacci F_0, F_1, F_2, ... ׻ƽϤץġ׽񤳤(Fibonacci ΤʤͤĴ٤褦)
ʲξΤȤ롥

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

Ūˤϡ㤨ХץA Υե̾ fibonacciA.rb Ȥȡ

 ruby -w fibonacciA.rb 30

ȤƼ¹Ԥȡ

F_0 = 0

F_1 = 1

F_2 = 1

F_3 = 2

F_4 = 3

F_5 = 5

F_6 = 8

F_7 = 13

F_8 = 21

F_9 = 34

F_10 = 55

F_11 = 89

F_12 = 144

F_13 = 233

F_14 = 377

F_15 = 610

F_16 = 987

F_17 = 1597

F_18 = 2584

F_19 = 4181

F_20 = 6765

F_21 = 10946

F_22 = 17711

F_23 = 28657

F_24 = 46368

F_25 = 75025

F_26 = 121393

F_27 = 196418

F_28 = 317811

F_29 = 514229

F_30 = 832040

Ȥ̤Ǥ褦ˤȤȤˤʤ롥

warning.png ץबĤǤ顤ξ¹Ԥơʤˤʤ뤳ȡסַ׻®٤㤦ȡפ򤭤ȳΤ褦 ޤץκפΰ㤤ˤĤƤͻƤ


warning.png (Ը) ƵǤʤּ¹®٤®ץץ񤤤Ƥߤ褦ɤФ褤.


ݡ

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

Ƽ

  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.


źեե: filesum.png 130 [ܺ] filegcd.png 150 [ܺ]