Ȼ/06

Top / Ȼ / 06

βˤĤ

βϡäѤ餤Τä⤯ͻҤñ˥ߥ졼ȤƽϤץȤΤäŪˤϡ

  1. ʤȤޤä˿ʤΨ 40%
  2. ʤȤФᱦ˿ʤΨϤ줾 30%
  3. 30ʬ餤ιƻϩο椫⤭Ϥ᤿
  4. (ջ)ޤ30ʬ餤ߥ졼Ȥ
  5. ⤴Ȥˡɤˤ뤫 print ʸǤäݤ?

ȤΤäơˤĤƼºݤˤäƤߤ褦

ץδߤΤǤϤʤɤΤ褦˺äƤ⤷褦
ξפʡ֥ġפΤǡ˽񤤤Ƥ

ץϺǽϾ񤯡
ơư֤ܾۤ˥פĤġĵǽɲä.

Τ꤫(㥤륽եȥȯɤ)ϸߤΥեȥؤǥץ೫ȯˡȤƽ׻뤵ƤˡΰĤǡʲΤ褦(⤢롥ַײ̵פΤηȸ)

  1. ʳŪ˥ץहΤǡн褹٤ʣޤ礭ʤɤ
  2. ˤŤ䤹нǽ
  3. ץब饨顼ӽΤǡ顼Ǻޤʤ
  4. ɤʳǤץϡưפΤǡ¿Ǥ(鿴ԤˤȤäƤä˿Ūˤ)

warning.png ºݤˡʲ˼ץ೫ȯʳǼ㤹٤Ƥ֥顼̵ưפȤܤ褦

ι¤ͤ

ޤι¤٤ȤȤǡξߤץι¤ͤƤߤ褦
Ȥꤢ30ʤࡤΰ⤴Ȥ˷ޤäơͻҤϤȤȤʤΤǡޤ

  1. include Math
  2.  
  3. # 30ʤ
  4. for step in 1..30 do
  5.   # ʤǷ
  6.   # ̤˽
  7. end

ʤ롥
줫顤ʤؿȡ̤˾֤ϤؿĤɤȤȤ狼Τǡؿ̾ƼΤ褦˲ɤ롥

  1. include Math
  2.  
  3. # ʤǷ
  4. def man_direction
  5. end
  6.  
  7. # ̤˽
  8. def man_print
  9. end
  10.  
  11. # ʲץ.
  12.  
  13. # 30ʤ
  14. for step in 1..30 do
  15.   # ʤǷ: man_direction
  16.   # ̤˽: man_print
  17. end

ǽ䤷Ƥ: Τ򤭤

ơ꤫餷ơ֤äƻΤɤդˤΤפ򵭲ƤѿɬפʤΤǡ
ѿ man_location ()Ȥơ㤨

  1. ü򥼥Ȥơ˥ץ饹ǿ
  2. 顤 15 餤?

ȤǤꤷ褦(򥼥ȤƤɤХꥨ󤬹ͤ)

ơȡؿ man_direction λĤ٤ޤ롥˿ʤȥץ饹 1 ꤷΤ顤

äιưman_direction ν
Ф+1
ޤä0
Ф-1

Ȥơ⤴Ȥ man_location ­ɤ
ȡäΰ֤狼в̽ϤǤϤ顤man_print man_location ˻ĤȤ

ȿǤȥץϼΤ褦˲ɤ롥

  1. include Math
  2.  
  3. # ʤǷ
  4. def man_direction
  5.   # ֱפ򤷤
  6.   # return 1
  7.   # ֤ޤäפ򤷤
  8.   # return 0
  9.   # ֺפ򤷤
  10.   # return -1
  11.  
  12.   # ˡ֤ޤäפϤƤ
  13.   return 0
  14. end
  15.  
  16. # ̤˽
  17. def man_print(loc)
  18. end
  19.  
  20. # ʲץ.
  21.  
  22. # äΰ֡ ǽˤϿ.
  23. man_location = 15
  24.  
  25. # 30ʤ
  26. for step in 1..30 do
  27.   # äΰ֤ man_direction η̤­.
  28.   man_location += man_direction
  29.  
  30.   # ̤˽: man_print
  31.   man_print(man_location)
  32. end

ơξ֤ǤɤץबưƤ뤫ʤˤƤߤΤǡman_print ñ˼褦
̤˳äݤΤ򺣤λǽϤǤʤƤ褤Τ顤ñ

  1. include Math
  2.  
  3. # ʤǷ
  4. def man_direction
  5.   # ֱפ򤷤
  6.   # return 1
  7.   # ֤ޤäפ򤷤
  8.   # return 0
  9.   # ֺפ򤷤
  10.   # return -1
  11.  
  12.   # ˡ֤ޤäפϤƤ
  13.   return 0
  14. end
  15.  
  16.  
  17. # ̤˽
  18. def man_print(loc)
  19.   print(loc, "\n")
  20. end
  21.  
  22. # ʲץ.
  23.  
  24. # äΰ֡ ǽˤϿ.
  25. man_location = 15
  26.  
  27. # 30ʤ
  28. for step in 1..30 do
  29.   # äΰ֤ man_direction η̤­.
  30.   man_location += man_direction
  31.  
  32.   # ̤˽: man_print
  33.   man_print(man_location)
  34. end

ȤƤߤ褦
warning.png ξ֤ǥץưƤߤȡ15 30ϤϤǤ(ޤưƤơ¿)

ǽ䤷Ƥ: ä󤬼˿ʤ뵡ǽ

ơäμΰǷؿ man_direction ˼ݤ
Ψ 1 ̤ˤ櫓櫓ǡ

probability-sm.png
Ψʬۤͻ

ȤʤäƤ뤳ȤͤơΤ褦˾ʬ뤳Ȥˤʤ(ʬΤ)
3̤ξʬϺγμ if ʽŤͤ뤳Ȥˤʤ롥

# ʤǷ
def man_direction
  r = rand # 򿶤Τϰ˰̤äƤ

  if (r < 0.3) then # ֺפ򤷤 
      return -1
    else 
      if (r < 0.7) then # ֤ޤäפ򤷤 
          return 0
        else # Ĥϡֱ
          return 1 
      end
  end
end

warning.png Ruby ǤϤΤ褦ʤȤ else if ̤ elsif Ƚ񤯤ȤǤ(ξ硤ǰʸʤΤ end ϰĸ)ΤǡȰʲΤ褦ˤʤ롥ưܼŪˤƱʤΤǡ٤Ф褤elsif 餯ɤߤ䤹ȥ֥⾯ʤ

# ʤǷ
def man_direction
  r = rand # 򿶤Τϰ˰. ̤äƤ

  if (r < 0.3) then # ֺפ򤷤 
      return -1
    elsif (r < 0.7) then # ֤ޤäפ򤷤 
      return 0
    else # Ĥϡֱ
      return 1 
  end
end


ơɤְִ㤤ˤĤդƤ
NG.png λȽΤ٤Ƥʬ rand ؿȤäưʲΤ褦ˤƤϤʤ

# ʤǷ
def man_direction
  if (rand < 0.3) then # ֺפ򤷤 
      return -1
    elsif (rand < 0.7) then # ֤ޤäפ򤷤 
      return 0
    else # Ĥϡֱ
      return 1 
  end
end

ʤʤ顤ȳΨʬѤäƤޤξ
: 30%, ޤä: 49%, : 21%
ˤʤäƤޤȰۤʤ븽ݤˤʤäƤޤǤ(ǺѤϤ rand ԤΤ̵̤ȤȤ⤢뤱ɤ).

warning.png ʤʤäƤޤ褯ͤ补

μδְ㤤ư򸫤ƤⵤŤˤ
ץ񤯤Ȥˤ褯ͤƵŤ!!

ơʳǥץ¹ԤȺǽ 15 ǡȤϾѤäƤФƤ
Ĥޤꡤץη׻ʬϤ⤦ȤȤǡȤϽϤθ˲᤮ʤ

ǽ䤷Ƥ: äΰֳ֤äݤ׽Ϥ뵡ǽ

Ȥ man_print εǽɤ
ȯۤȤƤϡ㤨 man_location 17 λ

 ----------------o-------------

ȤϤФС

  1. ޤ 16Ĥ "-" print
  2. ³ 1Ĥ "o" print
  3. 30-17=13Ĥ "-" print
  4. Ǹ˲Ԥ print

Ф褤
顤㤨Фľ˥ץߥ󥰤ơ

# ̤˽
def man_print(loc)

  for i in 1..(loc-1) do
    print("-")
  end

  print("o")

  for i in 1..(30-loc) do
    print("-")
  end

  print("\n")

end

ȤФ褤ϤǥץϴǤ롪 ưƤߤ褦˾̤Ϥ

warning.png Ruby ˤϷ줿ñ˷֤ʸˡ .times {} ̤ˤäơΥץϰʲΤ褦ˤ񤱤롥ȤԤϤǤ褤

# ̤˽
def man_print(loc)

  (loc-1).times {
    print("-")
  }

  print("o")

  (30-loc).times {
    print("-")
  }

  print("\n")

end

򰷤 :

ơޤǰäƤѿä¿äʸä꿿ͤäꤷΤΡֽפϰäƤʤä 礬Ȥ
ǡǽץǤɤˤĤƳؤܤ
ϡץߥ󥰸ǰäȤñ㤫ĴŪʽǤפˤĤƳؤܤ

ȤϡñǤ򽸤ƽȤΤǤ롥Ruby Ǥϡ

[ ] Ǥ ,() Ƕڤä¤٤

ˤʤ롥
warning.png Ruby ǤǤΡּפƱǤʤɤǤ¿ǤʸǤ⿿ͤǤ⺮ߤǤ롥

ơˤĤ͡Ǥ롥ɽˤƤߤȰʲΤ褦ˤʤ롥

ѿ̾ = Ǥ褤a = [1,5,2,-8]
ǤäƤ?.size ĤФ褤a.size ȤȾ 4 ˤʤ
[i-1] ѿ̾ˤĤa[3] ȤȾ -8 ˤʤ
Ǥɲunshift (Ƭɲäξ)a.unshift(6) ȤȾ a = [6,1,5,2,-8] ˤʤ
push (ɲäξ)a.push(6) ȤȾ a = [1,5,2,-8,6] ˤʤ
Ǥshift (ƬǤξ)a.shift ȤȾ a = [5,2,-8] ˤʤ
pop (Ǥξ)a.pop ȤȾ a = [1,5,2] ˤʤ
(֤)a = [1,5,2,-8], b = [3,1,-1,10,5] Ȥ
̽a & b Ȥa & b [1,5] ˤʤ
½a ¦ b Ȥa ¦ b [1, 5, 2, -8, 3, -1, 10] ˤʤ
a - b Ȥa - b [2, -8] ˤʤ
ña + b Ȥa + b [1, 5, 2, -8, 3, 1, -1, 10, 5] ˤʤ

ơĴƤ
ޤΡߤϤΡ5Ĥ 1.2, 3.0, 3.3, 4.1, 0.9 äơФǤ5ĤοʿѤ׻ץ򸫤Ƥߤ褦

  1. include Math
  2.  
  3. a = [1.2, 3.0, 3.3, 4.1, 0.9]
  4.  
  5. sum = 0.0
  6. for i in 1..(a.size) do
  7.   sum += a[i-1]
  8. end
  9.  
  10. average = sum/a.size
  11.  
  12. print(average,"\n")

ɤʸˡŪ񤷤Ȥä̵ϤΤñ

warning.png [Ը] ФƤʤˤԤȤϰճ¿ for ʸǹԤȾĹ
㤨СΥץǤ a ФƤ
for i in 1..(a.size) do
a[i-1] Ф
end
ȤƤ뤬1..(a.size) ȤΤϾŪפǤʤɡνˤ̵̤¿ǡΤ褦ʤäꤷ(iterator) Ruby ˤѰդƤ(ǶΥץߥ󥰸Ǥ iterator ¤ƤꡤȤƤǤ)

̾.each { |̾|
̾Ф(̾ a[i-1] ˤʤ)
}

# ĤΤȤϼʬѤʬ

νΰ̣狼ʤСɤɤȤȤ褤
ʤߤˤȤäƾΥץγ for ʸ񤭴

a.each { |e|
  sum += e
}

ʤɤȤʤ. ʤȤ֤ä񤱤פȤϸܤǤϤä狼


notes.png ơ5Ĥ 1.2, 3.0, 3.3, 4.1, 0.9 äơФǤ5ĤοΡִʿѡפ׻褦
warning.png a_1, a_2, a_3, ... a_n Ȥ n ĤοδʿѤȤϡ n ĤοѤȤ, (1/n)褷ΤǤ롥 Ĥޤꡤ(a_1 a_2 a_3 ... a_n)^(1/n) Τ.


ơץι¤ȤƼΤ褦ˤʤϤ

  1. include Math
  2.  
  3. a = [1.2, 3.0, 3.3, 4.1, 0.9]
  4.  
  5. # ơɤäƴʿѤ뤫͡
  6. # ʿѤˤĤƤϤäΤǡ򻲹ͤˡ
  7.  
  8. print(average,"\n")

οδʿѤʬϼʬǹͤƤߤ褦
ơΥץ¹Ԥȡ2.12994584474845 ϤǤ롥
warning.png 1.0 ˤʤäƤޤͤϲͤ褦ˤ褦ʤȤä?

notes.png ٥ȥȤȤ館ơĤΥ٥ȥΥ桼åѤ׻ؿäƤߤ褦.

  1. include Math
  2.  
  3. # ѡ٥ȥμƱΤȲꤷƤ롥
  4. def inner_product(va,vb)
  5.  
  6.   sum = 0.0
  7.   for i in 1..(va.size) do
  8.     sum += va[i-1] * vb[i-1]
  9.   end
  10.  
  11.   return sum
  12. end
  13.  
  14. # ʲץΡ
  15. a = [1.2, 3.0, 3.3]
  16. b = [2.1, -1.5, 0.2]
  17.  
  18. print(inner_product(a,b),"\n");

notes.png ˤȯŸơ(桼åŪ)٥ȥĹĤΥ٥ȥ֤γ٤׻ؿäƤߤ褦. ơĤΥ٥ȥĹ֤γ٤补
ξ硤줾 a Ĺ 4.618..., b Ĺ 2.588..., ֤γ٤ 1.681...(rad) ȤʤϤ



warning.png ҤˤǤ롥¿ȤƻȤפʤΤʤΤǶĴƤ

ҤˤǤ(ʤȤʤΤޤ)

ϽפäʤΤǾǫ˽񤳤㤨С

a = [[1,2,3],[4,5,6],[7,8,9]]

Ҥˤ a Ȥ褦
Ϥ礦ɹ

123
456
789

a Ȥơ줾Ǥˤ

a[0][0]a[0][1]a[0][2]
a[1][0]a[1][1]a[1][2]
a[2][0]a[2][1]a[2][2]

ǥǤˤʤ(ž֤ƹͤƤ⤤ɤ)
warning.png źؤΤ 1 ľΤ˺ȴְ㤨롥դ
ȤС󼡸ǡʤɤ¿ǡǰ롥פʱѤʤΤdzФƤ.

warning.png Ruby Ƿ줿ֺפΤˤ϶ϫ뤫⤷ʤΤǡƤ
㤨 n m 󷿤 a ˤϡ

a = Array.new( n ){ Array.new( m ){ 0 }}

ȤФ褤
ξȤ 0 ˤʤ롥ɬפ˱, ääƤʬ񤭴褦
for ʸ push ȤäƤؿäƤɤ

ž夲

ȥ٥ȥѡȹѤ׻ؿץߥ󥰤褦
ŪˤϰʲΤ褦ʤΤȤ롥

  1. Τ߰ȤƤ
  2. ΰ㤤ΥåϤʤƤ褤(󤷤Ƥ褤)
  3. (Ը) inner_product ؿѤƼ

ơ4 x 4 ιŬġ4٥ȥŬ˰ѰդơФѤη׻ޤȤǧ补
ʤߤˡ֤ޤפȤϡ׻פ뤳Ȥ򼨤ȤؤΤȤ롥

ݡ

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

Ƽ

  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.


źեե: fileprobability-sm.png 114 [ܺ]