https://www.acmicpc.net/problem/5666

 

5666번: Hot Dogs

In 2012 a new world record was set in the famous Nathan’s Hot Dog Eating Competition: the champion, Joey Chestnut, ate 68 hot dogs in ten minutes, an amazing increase from the 62 hot dogs eaten by the same Chestnut in 2011. Nathan’s Famous Corporation,

www.acmicpc.net


간단히 말하면 두 수를 입력받은 후 나눈 값을 소수점 두자리로 나타내면 된다.

하지만... 입력을 계속 받는 eof 처리를 할 필요가 있다.

루비에서 eof 처리를 어떻게 해야하나 알질 못했지만 이참에 한 번 알아봐야겠다 생각했다.

 

https://ruby-doc.org/core-3.0.0/IO.html#method-i-gets

 

Class: IO (Ruby 3.0.0)

set_encoding(ext_enc) → io click to toggle source If single argument is specified, read string from io is tagged with the encoding specified. If encoding is a colon separated two encoding names “A:B”, the read string is converted from encoding A (ext

ruby-doc.org

gets로 입력을 받으면 string 또는 nil이 반환된다.

nil은 null과 비슷한... 느낌으로 아무것도 없는 것을 말한다.

따라서 eof 즉 파일이 끝날 때는 아무것도 없으니 nil은 반환하니 잘 처리해주면 된다.

 

nil인지 아닌지 확인은 nil? 을 이용한다.

https://ruby-doc.org/core-3.0.0/Object.html#method-i-nil-3F

 

Class: Object (Ruby 3.0.0)

inspect → string click to toggle source Returns a string containing a human-readable representation of obj. The default inspect shows the object's class name, an encoding of its memory address, and a list of the instance variables and their values (by ca

ruby-doc.org

 

Ğ̎ĭ̎t̆̎H̆̎ŭ̎b̆̎ : https://github.com/YunYunYY/BOJ_Ruby/blob/main/B5666.rb

while (k=gets)
    if(k.nil?)
        break
    end
    a,b=k.split.map(&:to_f)
    puts ('%.2f' %(a/b))
end

 

재밌당...

'BOJ > Ruby' 카테고리의 다른 글

백준 Ruby : #12840  (0) 2022.05.06
백준 Ruby : #15734  (0) 2022.04.27
백준 Ruby : #9366  (0) 2022.04.19
백준 Ruby : #16675  (0) 2022.04.18
백준 Ruby : #14568  (0) 2022.04.15

+ Recent posts