๊ธฐ๋ก๋ฐฉ

BOJ_18238 : ZOAC 2 ๋ณธ๋ฌธ

CodingTest/Python

BOJ_18238 : ZOAC 2

Soom_1n 2022. 10. 19. 16:35

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

18238๋ฒˆ: ZOAC 2

2019๋…„ 12์›”, ๋‘ ๋ฒˆ์งธ๋กœ ๊ฐœ์ตœ๋œ ZOAC์˜ ์˜คํ”„๋‹์„ ๋งก์€ ์„ฑ์šฐ๋Š” ๋ˆ„๊ตฌ๋ณด๋‹ค ํ™”๋ คํ•˜๊ฒŒ ZOAC๋ฅผ ์•Œ๋ฆฌ๋ ค ํ•œ๋‹ค. ์ž‘๋…„ ZOAC์˜ ๋ฐฉ์‹์€ ๋„ˆ๋ฌด ์‹์ƒํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ ์„ฑ์šฐ๋Š” ๋ฌธ์ž์—ด์„ ๋ณด์—ฌ์ฃผ๋Š” ์ƒˆ๋กœ์šด ๊ทœ์น™์„ ๊ณ ์•ˆํ•ด

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์„ ์›ํ˜•ํŒ์—์„œ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์ตœ์†Œ๋กœ ์›€์ง์ผ๋•Œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

string = list(input())
idx = 0
answer = 0
for s in string:
    next = ord(s) - ord('A')
    diff = abs(next - idx)
    if diff <= 13:
        answer += diff
    else:
        answer += 26 - diff
    idx = next
print(answer)

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๊ณ„์‚ฐ์— ์ด์šฉํ•œ๋‹ค.
    • ์•„์Šคํ‚ค์ฝ”๋“œ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด 'A'๋ฅผ 0์œผ๋กœ ๋ฐ”๊พธ๋Š” ์‹์œผ๋กœ int๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
    • ํŒ์˜ ์ „์ฒด ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋Š” 26(์•ŒํŒŒ๋ฒณ)์ด๋‹ค. ๋งŒ์•ฝ ์ ˆ๋ฐ˜์ธ 13์ด์ƒ์˜ ๊ฑฐ๋ฆฌ์ฐจ์ด๋Š” ๋ฐ˜๋Œ€๋กœ ๋Œ๋ฆฌ๋Š”๊ฒŒ ๋น ๋ฅด๋‹ค
    • ๋ฐ˜๋Œ€๋กœ ๋Œ๋ฆฌ๋Š” ๊ฒฝ์šฐ๋Š” 26-์ฐจ์ด ๋กœ ๊ตฌํ•œ๋‹ค.
  • ์ด ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค. ๋ฐ˜๋Œ€๋กœ ์›€์ง์ด๋Š” ๊ฒฝ์šฐ๋ฅผ ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ํ’€๊ณ ๋ณด๋‹ˆ ๊ฐ„๋‹จํ•œ ์‹์œผ๋กœ ํ•ด๊ฒฐ๋๋‹ค.

728x90