λ°±μ€ 1174 μ€μ΄λλ μ νμ΄μ¬
2022. 12. 26. 05:46γπ± Algorithm/Back tracking
1174λ²: μ€μ΄λλ μ
μμ΄ μλ μ μλ₯Ό μμ§λ²μΌλ‘ νκΈ°νμ λ, μΌμͺ½μμλΆν° μ리μκ° κ°μν λ, κ·Έ μλ₯Ό μ€μ΄λλ μλΌκ³ νλ€. μλ₯Ό λ€μ΄, 321μ 950μ μ€μ΄λλ μμ΄κ³ , 322μ 958μ μλλ€. Nλ²μ§Έλ‘ μμ μ€μ΄λλ
www.acmicpc.net
μ‘°ν© νμ΄
from itertools import combinations
N = int(input())
result = set()
for i in range(1, 11):
for com in combinations(range(0, 10), i):
com = sorted(com, reverse=True)
result.add(int(''.join(map(str, com))))
result = sorted(result)
try:
print(result[N - 1])
except IndexError:
print(-1)
λ°±νΈλνΉ νμ΄
N = int(input())
result = set()
number = []
def dfs():
if number:
result.add(int(''.join(map(str, number))))
for i in range(10):
if not number or number[-1] > i: # μΌμͺ½ μκ° μ€λ₯Έμͺ½ μ λ³΄λ€ ν¬λ―λ‘ κ°μ μΌμ΄μ€μ ν΄λΉ
number.append(i)
dfs()
number.pop()
dfs()
result = sorted(result)
print(result[N - 1] if len(result) >= N else -1)
'π± Algorithm > Back tracking' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°±μ€ 1987 μνλ²³ νμ΄μ¬ (0) | 2022.07.08 |
---|