https://www.acmicpc.net/problem/18511
18511번: 큰 수 구성하기
첫째 줄에 N, K의 원소의 개수가 공백을 기준으로 구분되어 자연수로 주어진다. (10 ≤ N ≤ 100,000,000, 1 ≤ K의 원소의 개수 ≤ 3) 둘째 줄에 K의 원소들이 공백을 기준으로 구분되어 주어진다. 각
www.acmicpc.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from itertools import product
def sol(n,k,nums):
for i in range(len(str(n)),-1,-1):
prod = list(product(nums, repeat=i))
for j in prod:
tmp=''.join(map(str,j))
if n>=int(tmp):
return (tmp)
n,k =map(int,input().split())
nums=sorted(list(map(int, input().split())) ,reverse=True)
print(sol(n,k,nums))
|
cs |
product를 써서 모든 조합의 가지수를 만들어서 반복문으로 비교하면됩니다
Line 12번에서 reverse로 정렬한 이유는 큰 순서로 조합하기 위해서입니당
Using product , make all cases of combinations and compare the condition in for loop
In line 12 , reverse mean descending order (Bigger Number -> smaller)
반응형
'python-algorithm' 카테고리의 다른 글
백준 11866 요세푸스 문제 0 (0) | 2022.11.08 |
---|---|
Hacker rank Super Reduced String (0) | 2022.11.08 |
백준 1331 나이트 투어 (0) | 2022.11.08 |
백준 14582 오늘도 졌다 (0) | 2022.11.08 |
백준 8760 Schronisko (0) | 2022.11.08 |
댓글