Skip to content

Latest commit

ย 

History

History

BOJ_10951_A+B-4

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 
ย 
ย 

๋ฐฑ์ค€ #10951 A+B -4

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋”” ๋ฌธ์ œ ํ’€์ด์ž…๋‹ˆ๋‹ค.
  • ๋ฐฑ์ค€ 10951๋ฒˆ ์—์„œ ํ’€์–ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ์„ค๋ช…

  • ๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
  • ์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
  • ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10)

๋ฌธ์ œ ํŒ

  • โ˜†โ˜… [ํ•„๋…] A+B - 4 FAQ โ˜…โ˜†

    ์ด ๋ฌธ์ œ์˜ ๋ชฉ์ ์€ ํŒŒ์ผ์˜ ๋(EOF)์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํŒ๋‹จํ•˜๋Š” ๋ฒ•์„ ์—ฐ์Šตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ช‡ ์ค„์ด ์ฃผ์–ด์ง„๋‹ค ๋“ฑ์˜ ์ •๋ณด๋Š” ์ ˆ๋Œ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹จ์ˆœํžˆ ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ ๋‚ด์šฉ๋งŒ ์ ๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜์ง€ ์•Š์€ ์ƒํƒœ๊นŒ์ง€๋งŒ ๋ด์„œ๋Š” EOF๋ฅผ ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋” ์ด์ƒ ์ฝ์„ ๊ฒŒ ์—†์„ ๋•Œ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ฐฉ๋ฒ•์€ ์–ธ์–ด๋งˆ๋‹ค ๋‹ค๋ฅด๊ณ  ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ˆ, ๊ตฌ๊ธ€์— "C์–ธ์–ด EOF" ์™€ ๊ฐ™์ด ๊ฒ€์ƒ‰ํ•ด์„œ ๊ทธ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์„ธ์š”. ์ฃผ๋กœ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์œผ๋กœ ์„ค๋ช…๋˜์–ด ์žˆ๊ฒ ์ง€๋งŒ, ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ๋„ ํŒŒ์ผ ์ž…๋ ฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ตญ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

    1. (C/C++) scanf๋Š” EOF๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€, ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ฃผ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    2. (Java) Scanner์˜ ๋ฉ”์„œ๋“œ๋“ค์€ NoSuchElementException์„ ๋˜์ง‘๋‹ˆ๋‹ค.
    3. (Java) BufferedReader.readLine()์€ null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    4. (Python) input()์€ EOFError๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
    5. (Python) sys.stdin.readline()์€ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด1. input()

while True:
    try:
        A, B = map(int, input().split())
        print(A+B)
    except Exception as e:
        print(e) # EOF when reading a line
        break

ํ’€์ด2. sys.stdin.readline()

ํ’€์ด 1์—์„œ๋Š” EOF ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€๋งŒ, sys.stdin.readline()์€ ํŒŒ์ผ ๋์—์„œ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ try-except ๋ฌธ์—์„œ not enough values to unpack ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

from sys import stdin

while True:
    try:
        a, b = map(int, stdin.readline().split())
        print(a + b)
    except Exception as e:
        print(e) # not enough values to unpack (expected 2, got 0)
        break

๐Ÿ’ก

Python์œผ๋กœ ๋ฐฑ์ค€ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๋‚ด์žฅ ํ•จ์ˆ˜ input()์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ์˜ค๋‹ต์ฒ˜๋ฆฌ๊ฐ€ ๋˜๊ณ , sys ๋ชจ๋“ˆ์˜sys.stdin.readline()์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ์‹œ๊ฐ„ ์•ˆ์— ์ฑ„์ ์ด ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ž์ฃผ ๋ฐœ์ƒํ•œ๋‹ค.
๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์•„์•ผ ํ•  ๋•Œ, input()๋Œ€์‹  sys.stdin.readline()์„ ์ด์šฉํ•˜๋ฉด ์„ฑ๋Šฅ(์†๋„)์ด ํ–ฅ์ƒ๋œ๋‹ค.