-
[λ°±μ€] 1038 κ°μνλ μ with PythonPS 2022. 3. 23. 17:16728x90λ°μν
π BOJ 1038 κ°μνλ μ
π‘ 쑰건
μμ΄ μλ μ μ Xμ μλ¦Ώμκ° κ°μ₯ ν° μλ¦ΏμλΆν° μμ μλ¦ΏμκΉμ§ κ°μνλ€λ©΄, κ·Έ μλ₯Ό κ°μνλ μλΌκ³ νλ€.
Nλ²μ§Έ κ°μνλ μλ₯Ό μΆλ ₯νλ λ¬Έμ .
0μ 0λ²μ§Έ κ°μνλ μμ΄κ³ , 1μ 1λ²μ§Έ κ°μνλ μμ΄λ€. λ§μ½ Nλ²μ§Έ κ°μνλ μκ° μλ€λ©΄ -1μ μΆλ ₯νλ€.
Nμ 1,000,000λ³΄λ€ μκ±°λ κ°μ μμ°μ λλ 0μ΄λ€.
λ°±νΈλνΉ, λΈλ£¨νΈν¬μ€ μ νμ λ¬Έμ
π₯ μμ€ μ½λ
from sys import stdin dp = [x for x in range(10)] def solve(cnt, s, num): if cnt == len(s): dp.append(int(s)) return for i in range(num, -1, -1): if not s: solve(cnt, s + str(i), num - 1) else: if int(s[-1]) > i: solve(cnt, s + str(i), i - 1) for i in range(2, 11): solve(i, '', 9) dp.sort() n = int(stdin.readline()) if len(dp) - 1 < n: print(-1) else: print(dp[n])
π μμ λ° μ€νκ²°κ³Ό
μμ
18
μ€νκ²°κ³Ό
42
β¨οΈ λ¬Έμ νμ΄
μ€μ΄λλ μλ₯Ό ꡬν΄μ dp 리μ€νΈμ μ΄μ΄λΆμ¬μ€λ€.
0μμ 9κΉμ§λ 미리 dp μμ± ν λ λ£μ΄λ ν, μ¬κ·ν¨μλ₯Ό νΈμΆν΄ μ€μ΄λλ μλ₯Ό κ΅¬ν΄ λ£μ΄μ€λ€.
dp 리μ€νΈλ₯Ό μ λ ¬ν΄μ€ λ€, nμ΄ λ¦¬μ€νΈμ λ²μλ₯Ό μ΄κ³Όνλ€λ©΄ -1μ μΆλ ₯ν΄μ€λ€.
μ΄κ³Όνμ§ μλλ€λ©΄ dp[n] μ μΆλ ₯νλ€.
μ«μλ₯Ό μννλ©΄μ νλ κ²μ΄ μλ, λ¬Έμλ₯Ό μ΄μ΄λΆμ΄λ©΄μ μ€μ΄λλ μλ₯Ό λ§λ€λ©΄μ ꡬνλ©΄ μ½κ² ꡬν μ μλ€.
πΎ λλμ
- μ‘°κΈ ν€λ§¬λ» νμ§λ§ λ°λ‘ μ λ΅μ λ§μΆ μ μλ, ν°μ΄μ λΉν΄ λ³λ‘ μ΄λ ΅μ§ μμ λ¬Έμ μλ€.
λ°μν'PS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€] 1343 ν΄λ¦¬μ€λ―Έλ Έ with Python (0) 2022.03.29 [λ°±μ€] 1145 μ μ΄λ λλΆλΆμ λ°°μ with Python (0) 2022.03.29 [λ°±μ€] 12761 λλ€λ¦¬ with Python (0) 2022.03.23 [λ°±μ€] 11104 Fridge of Your Dreams with Python (0) 2022.03.22 [λ°±μ€] 10211 Maximum Subarray with Python (0) 2022.03.22