EOF

End Of File

EOF๋ž€ End Of File์˜ ์•ฝ์ž์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด ๊ทธ๋Œ€๋กœ ํŒŒ์ผ์˜ ๋์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ์š”. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์—์„œ๋Š” ์ž…๋ ฅ ๊ฐ’์ด ๋ช‡ ์ค„์ด๋‚˜ ์ฃผ์–ด์ง€๋Š”์ง€ ๋‚˜์™€์žˆ์ง€ ์•Š์œผ๋ฉด EOF๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

EOF ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์€ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” try / except๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ํ•˜๋‚˜๋Š” sys ๋ชจ๋“ˆ๊ณผ for๋ฌธ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

while True:
    try:
        blah blah
    except EOFError:
        break

try / except๋ฌธ์€ ์œ„์™€ ๊ฐ™์ด while๋ฌธ์œผ๋กœ ๋ฌดํ•œ๋ฃจํ”„ ์•ˆ์—์„œ EOF๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ try๋ฌธ์œผ๋กœ ๊ฐ’์„ ๋ฐ›๋‹ค๊ฐ€, EOF๊ฐ’์ด ๋‚˜์˜ค๋ฉด EOFError๊ฐ€ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— except๋ฌธ์— ์ ํ˜€์žˆ๋Š” break๋ฌธ์œผ๋กœ ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

import sys

for line in sys.stdin:
    blah blah

sys๋ชจ๋“ˆ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์€ ์œ„์™€ ๊ฐ™์ด for๋ฌธ๊ณผ sys.stdin์„ ํ†ตํ•ด ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ฃผ์˜ํ•  ์ ์€ ์ž…๋ ฅ ๊ฐ’์ด line ๋ณ€์ˆ˜์— ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— line์„ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋””๋ฒ„๊น…์„ ํ•  ๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ EOF๋ฅผ ํ•˜๋ ค๋ฉด Ctrl + D ํ˜น์€ Ctrl + Z๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์—ฐ์Šต๋ฌธ์ œ

BOJ 11718

๋ฌธ์ œ์—์„œ ์ž…๋ ฅ์ด ์ตœ๋Œ€ 100์ค„์ด ์ฃผ์–ด์ ธ ์žˆ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋Š”๋ฐ์š”. ์ด ๋ง์€ ์ตœ๋Œ€ 100์ค„์ด๋ฏ€๋กœ ์ž…๋ ฅ ๊ฐ’์ด ํ•œ ์ค„๋งŒ ๋‚˜์˜ค๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ์ˆ˜๋„ ์žˆ๊ณ , 50์ค„๋งŒ ๋‚˜์˜ค๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋ช‡ ์ค„์ธ์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์ฃผ์–ด์ง€์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ EOF๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ์ž…๋ ฅ ๊ฐ’์„ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ input()์œผ๋กœ ๊ฐ’์„ ๋ฐ›๊ณ  ๋ฐ”๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ฒ ๋„ค์š”.

while True:
    try:
        k = input()
        print(k)
    except EOFError:
        break
import sys

for k in sys.stdin:
    print(k)

BOJ 10823

์ด ๋ฌธ์ œ๋„ ์ •ํ™•ํ•˜๊ฒŒ ๋ช‡ ์ค„์˜ ๊ฐ’์„ ๋ฐ›๋Š”์ง€ ๋‚˜์™€์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ ์˜ˆ์ œ๋„ ๋ณด๋‹ˆ๊นŒ ์ž…๋ ฅ ๊ฐ’๋„ ์ด์ƒํ•˜๋„ค์š”.

์ž…๋ ฅ ๊ฐ’์„ ๋ณด๋‹ˆ๊นŒ ๋ฌธ์ž์—ด์˜ ์—ฐ์‚ฐ์ž์ธ +๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด ๋ณ€์ˆ˜์— ์ž…๋ ฅ ๊ฐ’์„ ๊ณ„์† ๋ถ™์—ฌ์ฃผ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋„ค์š”.

s = ''
while True:
    try:
        line = input()
        s += line
    except EOFError:
        break
import sys

s = ''
for line in sys.stdin:
    s += line

์ด๋ ‡๊ฒŒ ๊ฐ’์„ ๋ถ™์—ฌ์ฃผ์—ˆ์œผ๋ฉด ์ด์ œ split(',')๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฝค๋งˆ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ˆซ์ž๋ฅผ ๋‚˜๋ˆ ์ฃผ๊ณ , sumํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

s = ''
while True:
    try:
        line = input()
        s += line
    except EOFError:
        break
arr = list(map(int, s.split(',')))
print(sum(arr))
import sys

s = ''
for line in sys.stdin:
    s += line
arr = list(map(int, s.split(',')))
print(sum(arr))

ํ’€์–ด๋ณผ ๋ฌธ์ œ

EOF ๋ฌธ์ œ๋Š” try / except๋‚˜ for๋ฌธ + sys๋ชจ๋“ˆ์ค‘ ์•„๋ฌด๊ฑฐ๋‚˜ ํŽธํ•œ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Last updated