λ°±μ€ 1174 μ€μ΄λλ μ νμ΄μ¬
2022. 12. 26. 05:46γπ± Algorithm/Back tracking
μ‘°ν© νμ΄
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 |
---|