-
๋ฐฑ์ค 12605 : ๋จ์ด์์ ๋ค์ง๊ธฐ (print ์ฑ๋ฅ)๐ Algorithms 2023. 2. 20. 15:09
Algorithms & Data structures
๐ ์คํฐ๋ ์ ๋ณด
graceful-canary-e9f.notion.site
๐ ๋ฌธ์ ์์ฝ
์ ๋ ฅํ ๋ฌธ์ฅ์ ๋์ด์ฐ๊ธฐ ๊ธฐ์ค์ผ๋ก ์ชผ๊ฐ ํ ๋ฐ๋์ ์์๋๋ก ์ถ๋ ฅํ๋ ๋ฌธ์ .
- ์ ๋ ฅ
์ฒซ์งธ์ค - ํ ์คํธ ์ผ์ด์ค ์
n - ๋ค์ง์ ๋ฌธ์ฅ ๋ฌธ์ฅ
- ์ถ๋ ฅ
๋ฐ๋ ์์๋ก ์ถ๋ ฅ
๐ ๋ฌธ์ ํ์ด
- ์ฌ์ฉ ์๋ฃ๊ตฌ์กฐ ๋ฐ ์๊ณ ๋ฆฌ์ฆ
- ์คํ
- ๋ฐฐ์ด ๋ด ์์๋ฅผ ๋ค์ง๊ธฐ ์ํด ๋ง์ง๋ง ์์(last-in)๋ฅผ ๋จผ์ ๊บผ๋ด์ผํ๋ค(last-out)๋ ์๊ฐ๊ณผ ํจ๊ป ์คํ์ ์ฌ์ฉ
- ์คํ
- ๋ฌธ์ ํ์ด
- ๊ฒ์ฆ๋์ง ์์ ๊ฐ์
popํ ์์๋ฅผ ๋ด์ **์๋ก์ด ์คํ์ ์์ฑํจ์ผ๋ก์จ ๋ ํฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์งํ ๊ฒ(X)**์ด๋ผ ์๊ฐ → ๋ฐฉ๋ฒ 2๋๋ก, popํ ์์๋ฅผ ๊ฐํ๋ฌธ์ ์ ๊ฑฐ ํ ๊ทธ๋ ๊ทธ๋ ํ๋ฆฐํธํ์ฌ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ฝํ ์ ์์ ๊ฒ์ด๋ผ ์๊ฐ → ํ์ง๋ง ๋ฐฉ๋ฒ 1๊ณผ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ๋์ผํ๊ณ , ์๊ฐ์ด ์คํ๋ ค print๊ตฌ๋ฌธ์ ๋ง์ด ์ฌ์ฉํ์ ๋ ๋ ์์๋จ์ ํ์ธ.
# ๋ฉ๋ชจ๋ฆฌ 113112KB # ์๊ฐ 112ms # ๋ฐฉ๋ฒ 1. import sys input = sys.stdin.readline N = int(input()) for i in range(1, N+1): char_lst = input().rstrip().split() print(f"Case #{i}:", end= " ") while char_lst: print(char_lst.pop(), end=" ") # ํ๋ฒ์ ํ๋ฆฐํธ or ์ฌ๋ฌ๋ฒ ํ๋ฆฐํธ!
# ๋ฉ๋ชจ๋ฆฌ 113112KB # ์๊ฐ 108ms # ๋ฐฉ๋ฒ 2. **์ฑํ ๋๋ฅ** import sys input = sys.stdin.readline N = int(input()) for i in range(1, N+1): char_lst = input().rstrip().split() stk = [] while char_lst: stk.append(char_lst.pop()) print(f"Case #{i}:", " ".join(stk))
- ์ด์ ๊ฐ์ ์ฐธ์กฐํ ์๋ก์ด ์คํ ์์ฑ
์๋ ์์ ๋ฅผ ๋ง๋ค์ด๋ณด์์ต๋๋ค. ์ด๋ฏธ ์์ฑ๋ ๊ฐ(๋ณ์,์คํ,๋ฐฐ์ด)์ ์ฐธ์กฐํ์ฌ ์๋ก์ด ๊ฐ์ ์์ฑํ๋ ๊ฒฝ์ฐ, ๊ธฐ์กด ์์ฑ๋ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๊ฐ ๋ด๊ธฐ๊ฒ ๋ฉ๋๋ค.
๋ฐ๋ผ์, ํด๋น ๋ฌธ์ ์ฒ๋ผ ์ด์ ์คํ๊ฐ์ ์ฐธ์กฐํด ์ญ์์ ์ ์คํ์ ์์ฑํ๋ ๊ฒฝ์ฐ์ ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ์๋ชจ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค.
stk1 = [1,2,3,4] stk1_cp = stk1.copy() stk2 = [] while stk1: stk2.append(stk1.pop()) for x,y in zip(stk1_cp, reversed(stk2)): print("๋ฉ๋ชจ๋ฆฌ ์ฃผ์ ๋์ผ" if id(x) == id(y) else "์์ด") # id(๋ณ์): ๋ฉ๋ชจ๋ฆฌ ์ฃผ์ ํ์ธ
- ๋ฐฐ์ด ์
๊ฒ์ฆ์๋ ๊ฐ์ ์ ๊ธฐ๋ฐ์ผ๋ก ํ ์๊ฐ์ ์ํํ๋ค. ๋ง์ ๋ฌธ์ ๋ค์ ์ฐ์ตํด๋ณด๋ฉฐ ๊ฐ์ ์ ๊ฒ์ฆํด๋ณด์.
'๐ Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 10828 : ์คํ (+ํ์ด์ฌ์์ switch-case ๊ตฌํ) (0) 2023.02.20 ๋ฐฑ์ค 6603 ๋ก๋ (์ฌ๊ท๋ก ๋ถ๋ถ์งํฉ/์์ด/์กฐํฉ ๊ตฌํ ์ฐ์ต) (0) 2023.02.20