Ȼ/11

Top / Ȼ / 11

βˤĤ

ž夲Ǥʤβ̤񤷤Ȼפä⤤褦ʤΤǡ⤷褦

3ܿ or 3ĤˤĤƤΤüɽ

ϴñ
ޤǫʥ򸫤褦(ûȤǤ뤷ץΤ򸫤褦)

  1. include Math
  2.  
  3. # (3 ܿ or3 Ĥ) åɽƤޤ
  4. def check(n)
  5.  
  6.   # 3 Ĥɤǫ˥åʤ, 3 ޤޤʤä?פȹͤơ
  7.   if (n.to_s.index("3") == nil) then
  8.       with_three = false
  9.     else
  10.       with_three = true
  11.   end
  12.  
  13.   # Ȥ 3 ܿɤȤ碌ƥå
  14.   if ((n % 3 == 0) || with_three ) then
  15.       print("!!!! \n")
  16.     else
  17.       print(n,"\n")
  18.   end
  19. end
  20.  
  21. # ʲץ
  22.  
  23. # ޤͤ餦
  24. n_upper = ARGV[0].to_i
  25.  
  26. # 1 ˥å & ɽ
  27. for n in 1..n_upper do
  28.   check(n)
  29. end

Ǥ index ޥɤȤäƤߤ¾ˤ⤤Ȥ롥
ȤRubyβ(侩ƤϤ)ˤϡʸޤޤƤ뤫פΤ뤿ΤФˡ .include?() ʤɤ񤤤Ƥ롥
Ȼפͤ⤤뤫⤷ʤɤ餫ȤȤϤ

鿴Ԥθβ̵
ץ񤯤Τ̵뤫᤯㤨

ȤȤʤĤȻפΤ褵

(Ը 1) 3 ޤޤƤ=index("3") η̤פȤǤʤ

  # 3 Ĥɤǫ˥å
  if ( n.to_s.index("3").is_a?(Integer) ) then
      with_three = true
    else
      with_three = false
  end

ȤƤ褤
ϡ֡˽°ޤ?פȤΥޥ .is_a?() ȤäƤ롥

(Ը 2) ⤽ nil false Ʊ(󵭽)ʤΤ

äȼΤ褦˽񤱤롥Ȥ줬ƻʡ

# (3 ܿ or3 Ĥ) åɽƤޤ
def check(n)
  if ((n % 3 == 0) || (n.to_s.index("3"))) then
      print("!!!! \n")
    else
      print(n,"\n")
  end
end


դοǿ?

Ȥꤢι¤ͤ褦

  1. include Math
  2.  
  3. # ǿɤå(Υԡ)
  4. def is_prime(n)
  5.   answer = true
  6.   for i in 2..sqrt(n).floor do
  7.     if (n % i == 0) then
  8.       answer = false
  9.       break
  10.     end
  11.   end
  12.  
  13.   return answer
  14. end
  15.  
  16. # Ҥä֤
  17. def inv(n)
  18.   # ϸ
  19.   return n
  20. end
  21.  
  22. # οȡҤä֤ǿɤå
  23. def inv_couple(n)
  24.   if (is_prime(n) && is_prime(inv(n))) then
  25.     return true
  26.   end
  27.   return false
  28. end
  29.  
  30. # ʲץ
  31.  
  32. # Ĵ٤ϰϤξ¤餦
  33. n_upper = ARGV[0].to_i
  34.  
  35. # 11ʾ塤°ʲޤǥå
  36. for i in 11..n_upper do
  37.   if inv_couple(i) then
  38.       print("(",i, ", ",inv(i),") are prime numbers.\n")
  39.     else
  40. #      print(i,"\n")
  41.   end
  42. end

򸫤ȡʤȤʤؿϿҤä֤ʬǤ롥
ǡäͤФ褤
ȤϤ֤Ҥä֤ʤΤǴñǡ㤨мΤ褦ˤʤ

# Ҥä֤
def inv(n)
  #äʸľƤ
  in_st = n.to_s
  # ĤǤˤʸĹФƤ
  len = in_st.length

  # ޤäݤʸѰդ
  ans_st = ""

  # ȤϡäǸ夫դ­Ƥ
  for i in 1..len do
    ans_st << in_st[len-i]
  end

  # ȤϿᤷ֤
  return ans_st.to_i
end

ޤ褯ߤв񤷤ȤϤʤ
ޤRuby ܤ򸫤Фäȴñˡ뤳Ȥ˵Ť


ƥ

ȻʳʬƤäϤޤȤƤޤޤϥ롼ƷǤ褦ʲ̤

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

  1. X Y Ȥ򼡤Τ褦롥
    1. ⤷ a_i = b_i ʤ X_i = 1, Ǥʤ X_i = 0.
    2. a_i = b_i ǤϤʤb_i Ʊ a_j ʤ Y_i = 1, Ǥʤ Y_i = 0.
    3. X = X_i ιסY= Y_i ι
  2. n, m Фơ X, Y ׻Ϥؿ xy(n,m) ץˤ롥

ơɬפʤΤϼΤ褦ʥץ

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

ơɤͤ褦ޤϤȤ⤫ι¤

  1. include Math
  2.  
  3. # n, m ä X, Y ֤
  4. def xy(n,m)
  5.   # ϸǡ
  6.   return n, m
  7. end
  8.  
  9. # ꤵ줿οǺ
  10. def make_n(digit)
  11.   # ϸ
  12.   return 0
  13. end
  14.  
  15. # ʲ
  16.  
  17. # 餦
  18. digit = ARGV[0].to_i
  19.  
  20. # ˤꤵ줿 n
  21. n = make_n(digit)
  22.  
  23. # ץưǧΤˡޤǤɽƤޤ()
  24. print("Ans: ", n, "\n")
  25.  
  26. # ʲϼ
  27. print("m? : ")
  28. while (s = $stdin.gets) do
  29.   s.chomp!
  30.   m = s.to_i
  31.  
  32.   # X, Y 롥
  33.   x,y = xy(n,m) 
  34.  
  35.   # פʤ餪ǤȤǡǤʤ x,y ɽ&ƥ
  36.   if (x == digit) then
  37.       print("Conguratulaions!\n")
  38.       # while æ
  39.       break;
  40.     else
  41.       print("X: ", x, ", Y: ", y,"\n")
  42.   end
  43.  
  44.   print("m? : ")
  45. end

ʤߤˡbreak Ȥ̿ȤäƤ뤬 while for ʤɤΥ롼פæФ̿Ǥ롥ξ while γȴΤǡץκǸˤɤ夭λ뤳Ȥˤʤ롥
break ȤʤˡǤɤȤСä? ȤǼƤѿ(ѿե饰ѿȤ)Ѱդʤɤͤ롥


ꤵ줿ο

Ϥ뤬Ť˹ͤ񤷤ʤ
ȤС 4ο = 1000ʾ 10000̤ = 1000 + (0ʾ9000̤) = 10^3 + (0ʾ 9*10^3 ̤) 򤹤Сñ˼Τ褦˺롥

# ꤵ줿οǺ
def make_n(digit)
  n = 10**(digit-1)
  return n + rand( 9 * n )
end

ޤˡפĤʤäȤͤϡ⤽ 4ο = 1000ʾ 10000̤ȤǧǤʤäΤǤϤʤ
ȤȤʤΤȤ򤤤ˤªƤʤ ȤȤʤΤǡȿʤɬפ


ˤսο夲

ơο X,Y ˡ
䤳˱äƲȤϡ⤽򤷤Ƥ뤫ŤˤʤͤФʤʤޤץबְ㤨ʤ褦ˤɤ뤫Ȥפɬפ
դ˸Сñ˿夲ƤȾΤ줫μԤ롥
ĴƤ

䤳ФƹפʤǤɤ
ԤΨ˹⤤

Ȥ˺ϥȤƤϤʤʤ򥫥ȤƤޤʣȥߥפ¿ȯǤ롥⤦ǫ˸С֥åƳȤ狼ä顤Ϥϥå˻Ȥʤ׿ΤǡΤȤɤäƳμ¤˼¹Ԥ뤫ȤȤˤʤ롥ϡִ˥åս⤦åʤ褦˥ޡ󥰤Ƥޤ׹פͭ
ǫ˸ȡޤ

֤ιǤϽʣȥߥɤʤ

Τǡ


ʣȥߥɤˤϡ
⤦ʤȥǡ˥ޡƽ񤭴Ƥޤ

ȤȤˤʤ롥

ơǤϤΥޡ󥰤ɤԤΤºݤΥɤ򸫤򤷤褦

def xy(n,m)
  n_st = n.to_s
  m_st = m.to_s

  size = n_st.size

  # ʸ֤פ륱å
  for i in 1..size do
    if (n_st[i-1] == m_st[i-1]) then
      n_st[i-1] = "x" # פ餽ξ x ȥޡǤʸϤ⤦ǤϤʤ.
      m_st[i-1] = "-" 
        # m ΤʸϤ⤦ץȤ˻Ȥʤְ㤨ƽʣȤʤ褦
        # n ΤɤʸȤפʤʸƤ
    end
  end

  # x ȤʤäƤս
  x = 0
  for i in 1..size do
    if (n_st[i-1].chr == "x") then
      x += 1
    end
  end

  # ʸ֤Τߡפפ륱å
  for i in 1..size do
    for j in 1..size do
      if ( (m_st[i-1] == n_st[j-1])  ) then
        n_st[j-1] = "y" # פ餽ξ y ȥޡ. ǤʸϤ⤦ǤϤʤ.
        m_st[i-1] = "-" 
          # m ΤʸϤ⤦ץȤ˻Ȥʤְ㤨ƽʣȤʤ褦
          # n ΤɤʸȤפʤʸƤ
      end
    end
  end

  # y ȤʤäƤս
  y = 0
  for i in 1..size do
    if (n_st[i-1].chr == "y") then
      y += 1
    end
  end

  # ̤֤
  return x, y
end

С񤷤ͤ뤳Ȥʤְ㤨ʤ褦˽롥
Ȥ˼ԤץˤʤäƤޤäͤϡνǼʬʤˤľƤߤ褦


եϡե

ơ⾯饤ȤƤǤ롥
ޤǡǡϤϡܡ, ̡⤷ϥ쥯( | <, > Ȥ)ˤĤƤΤ߳ؤǤˤʣΥեƱ˰ʤȤ³롥
ޤʥե򰷤ݤϡץ¦Ǻǽ˥Хååפʤɤθ⤷
̣Ǥ⡤եϤȺ¤ΥեˤĤƳؤܤ

ե

ޤϥե֥ԡסְưס̾ѹס־äפʤɤ, եˤĤƳؤܤ
餬Ǥ褦ˤʤȡեΥХååפ⼫ͳߤ

ơˤĤƤϡRuby fileutils ⥸塼ȤΤ褤. Ǥޤ

եԤݤϡץκǽ
require "fileutils" ȤԤ褦

ơΤޤʤƤ硤Τ褦ʥեޥɤ褦ˤʤ롥

ե ʰɽ

ʸˡ
(¸ߤʤ)եFileUtils.touch(A)äݤΥե뤬롥¸ߤեꤷϡΥեι򿷤
ե륳ԡFileUtils.cp(A,B)A B ˥ԡ. B ϥե̾Ǥʤǥ쥯ȥ̾Ǥɤ
եưFileUtils.mv(A,B)A B ˰ư. B ϥե̾Ǥʤǥ쥯ȥ̾ǤɤA B äƤǥ쥯ȥ꤬Ʊȡñʤ̾ѹȤȤˤʤ
եõFileUtils.rm(A)
ǥ쥯ȥFileUtils.mkdir(A)
¾

ơ褦

Υ롼̾줿ե̤˺

㤨Сtest_00.dat, test_01.dat, ... , test_30.dat Ȥ褦˥ե򿷤˺ɬפȤ褦
ϡΤ褦ʥץȤळȤˤʤ롥

  1. include Math
  2. require "fileutils"
  3.  
  4. def make_fn(n)
  5.   if (n > 9) then
  6.       st = n.to_s
  7.     else
  8.       st = "0" + n.to_s
  9.   end
  10.  
  11.   return "test_" + st + ".dat"
  12. end
  13.  
  14. for i in 0..30 do
  15.   FileUtils.touch(make_fn(i))
  16. end

notes.png ʹߤμ½ΤˡΥץ¹ԤƤ


Υ롼̾줿ե򥳥ԡ

Ǥϡ嵭Ǻäե31ĤΥХååסʤԡȤɬפ줿Ȥ褦
ԡ̾ޤäƤơtest_00.dat, test_01.dat, ... , test_30.dat Ȥ̾򤽤줾 backup_00.dat, backup_01.dat, ... , backup_30.dat Ȥ̾ˤƥХååפ

notes.png Ū¸ץ񤤤ơ¹Ԥ褦

ŪˤϡΥץ bk.rb Ȥȡ

 ruby -w bk.rb

Ȥƥץ¹Ԥȡ⤦31ĤΥե뤬 backup_00.dat,backup_01.dat, ... , backup_30.dat ȤƸȤȤˤʤ롥


Υ롼̾줿եõ

notes.png μ½ǤǤ62ĤΥե test_00.dat, test_01.dat, ... , test_30.dat, backup_00.dat, backup_01.dat, ... , backup_30.dat õץ񤤤ơ¹ԤƤΥեä



ǡ󶵤äʸȹ碌ХեּͳǤ뤳Ȥˤʤ롥


ե

ơ٤ϼʬ̾ꤷեȤɤ()Υե˲񤭹()뤳Ȥؤܤ

ޤեϤˤäƤ«񤤤Ƥ
եϤΰ٤ˤϡι԰٤ɬפǤ롥

եϤˤ open ޥɤμ¹Ԥ,
ˤ close ޥɤμ¹ԤɬפǤ

ʲ⤷褦


open ޥ: եȤ

եȤˡΥե֤ɤȤΤפơեѿ ˷ӤĤɬפ롥
ʸˡϰʲΤȤǤ롥


եѿ = open(ե̾,⡼ɵ)

open ΤȤˡ줿եѿФ gets print ΤȤȤˤʤ롥
ޤ⡼ɵϰʲΤȤǤ롥ʬˤ褯褯դ褦

open Υ⡼

"r"ɤ߹ߤΤ⡼ɤꤷʤȤˤʤ
"r+"ɤ߽ξ
"w" 񤭹˥ե뤬äݤˤΤա
"w+" ɤ߽ξ˥ե뤬äݤˤΤա
"a"ɲý񤭹
"a+"ɲý񤭹ߡɤ߹

Ȥꤢ⡼ "w" "w+" դȳФƤ


close ޥ: եȤä

եȤä顤ΥեֻȤäפƥեͳˤƤɬפ롥
ʸˡϰʲΤȤ, ϴñǤ롥


եѿ.close



ơοϤǤ뤬ޤǤ˳ؤΤۤܤΤޤ޻ȤơʲΤ褦ˤʤ롥


gets ޥ: ե뤫ɤ߹()

ȤϴñǡˡФƬ եѿ. ʲΤ褦դǤ롥


եѿ.gets


print ޥ: եؽ񤭹()

Ʊͤ˰ˡФƬ եѿ. ʲΤ褦դǤ롥


եѿ.print(񤭹ߤ)



ơեϤФƥԥʤ⤢顤ʲƤߤ褦
notes.png ʲʬǤºݤ˹Ԥä򤷤褦

㤨Сinput.dat Ȥ̾Υեˡ

Your apple is large and my apple is small.

His orange is yellow.

Ȥʸ񤫤ƤȤ롥ơʸ apple lemon ˽Τ output.dat Ȥե˽񤭹ळȤͤ褦
ȡΥץ㤨аʲΤ褦ˤʤ롥ɤ򤷤褦

  1. include Math
  2.  
  3. # , Ѥˤ줾ե open
  4. in_fn = open("input.dat","r")
  5. out_fn = open("output.dat","w")
  6.  
  7. # Ԥ¤ѥե뤫ɤ߹ (while ȤȤ߹碌ϰä)
  8. while (line = in_fn.gets) do
  9.   line.chomp!
  10.  
  11.   # ʸ֤(μ)
  12.   out_line = line.gsub("apple","lemon")
  13.  
  14.   # ѥե˽
  15.   out_fn.print(out_line,"\n")
  16. end
  17.  
  18. # äΤǡΥե
  19. in_fn.close
  20. out_fn.close

ºݡΥץư output.dat Ȥե뤬˺(ǤˤȤ˴)

Your lemon is large and my lemon is small.

His orange is yellow.

ȤȤˤʤäƤϤ
ơϤ˴ؤƤϺޤǤ gets print λȤȤۤƱȤ褯狼

ž夲

եȥեϤξȤɬפ롤ξץ񤳤
ϰʲ̤Ǥ롥

  1. ѤΥǡäƤե̾Ĥεưѥ᡼ȤƤ餦
  2. ¹ԻˤޤϺǽ˥եΥХååפԤ
  3. Ĥεưѥ᡼1ʸǤ롥ʸ c ɽƤȡץϥǡγƹԤʸ c ʸޤǤ뤫Ĵ١output.dat Ȥե˹ֹȶ˽Ϥ.

Ū˹ԤȤĤΤ褦ˤʤ
ޤ㤨Сinput.txt Ȥեˡ web Meet the President (֤褦ĹءפαѸ)Ĺΰʸΰ

The motto "Live Locally, Grow Globally" at Osaka University has a deep meaning

beyond the historical significance of the university's roots which reach back to

Kaitokudo1 and Tekijuku2. These two establishments were not only places of

learning open to the public, they were also schools possessing cutting-edge

knowledge in their day, places of unimpeded study for citizens. The

inspirational spirit of Kaitokudo and Tekijuku defines the future course for

Osaka University, an institution imparting "knowledge" both locally and

globally.


Osaka University has always emphasized: (1) Cutting-edge research, (2) the

creation of, in the University's tradition, new research fields by amalgamating

previously unknown fields, (3) an atmosphere that reveres study and research

(which is why we have been called a "University of Education"), (4) respect for

a liberal arts education at our graduate schools, and (5) active contributions

to society encouraged through university-industry cooperation. Now it's time for

the University to embrace this unique education and research style as the

definitive "Handai style [Osaka University style]."


Osaka University strives to nurture researchers and professionals engaged in

cutting-edge research, doing so with the full trust of society. In other words,

producing people with true wisdom, refinement, and the power to design and

communicate. Centering on these characteristics and looking to the future, the

University aims to become a university looking ahead as times change; not a

university only for one nation or only for researchers, but a university walking

forward in harmony with the citizens of Osaka. This is the style of "knowledge"

we aim to impart. Osaka University's staff, students, researchers join hands to

build such a university, and the finest evaluation we can receive is when

society looks and says, "People who've studied at Osaka University really are

one step ahead."

ȤƤΤϥǡեȤ褦
ơʬν񤤤ץब㤨 c_count.rb Ȥȡʸ e ο夲

 ruby -w c_count.rb input.txt e

ȤƹԤ碌ȡinput.txt ƱȤ input.txt.bak ȤХååץե뤬졤 output.dat Ȥե

1 : 6

2 : 6

3 : 8

4 : 9

5 : 8

6 : 6

7 : 3

8 : 0

9 : 0

10 : 8

11 : 7

12 : 9

13 : 8

14 : 4

15 : 6

16 : 10

17 : 5

18 : 0

19 : 9

20 : 7

21 : 9

22 : 9

23 : 7

24 : 6

25 : 6

26 : 6

27 : 9

28 : 8

29 : 3

Ȥǡ񤭹ޤƤ롤Ȥ̤ˤʤ롥


ݡ

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

Ƽ

  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.