728x90
반응형

개발 82

[Baekjoon 15665 / Python / 실버2] N과 M (11)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()result = [-1] * Mdef backTracking(depth):    if depth == M:        print(' '.join(map(str, result)))        return        temp = 0    for i in range(N):        if temp != numbers[i]:            temp = numbers[i]            result[depth] = numbers[i]            backTracking(d..

[Baekjoon 15664 / Python / 실버2] N과 M (10)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()visited = [False] * Nresult = [-1] * Mdef compare(a, b):    if a > b:        return False    return Truedef backTracking(depth):    if depth == M:        print(' '.join(map(str, result)))        return        temp = 0    for i in range(N):        if (depth == 0 and temp != n..

[Baekjoon 15663 / Python / 실버2] N과 M (9)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())visited = [False] * Nnumbers = list(map(int, input().split()))numbers.sort()result = [-1] * Mdef backTracking(depth):    if depth == M:        print(' '.join(map(str, result)))        return        # temp 변수를 활용하여 완성된 중복 순열을 방지한다.    temp = 0     for i in range(N):        if not visited[i] and temp != numbers[i]:            resu..

[Baekjoon 15657 / Python / 실버3] N과 M (8)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()box = []def compare(a, b):    if a > b:        return False    return Truedef backTracking(depth):        if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        if depth == 0 or compare(box[depth-1], numbers[i]):            box.ap..

[Baekjoon 15656 / Python / 실버3] N과 M (7)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()box = []def backTracking(depth):    if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        box.append(numbers[i])        backTracking(depth + 1)        box.pop()backTracking(0) 백트래킹( 사실 백트래킹도 아니다 )을 통해 모든 수열을 만들어 출력하기만 하면 된다. 중복 수..

[Baekjoon 15655 / Python / 실버3] N과 M (6)

import sysinput = sys.stdin.readline# 중복 없으며 뒷 숫자가 더 커야 한다N, M = map(int, input().split())numbers = list(map(int, input().split()))numbers.sort()box = []def compare(a, b):    if a >= b:        return False    return Truedef backTracking(depth):    if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        if depth == 0 or (numbers[i] not in box and compar..

[Baekjoon 15654 / Python / 실버3] N과 M (5)

import sysinput = sys.stdin.readlineN, M = map(int, input().split())numList = list(map(int, input().split()))numList.sort()box = []def backTracking(depth):    if depth == M:        print(' '.join(map(str, box)))        return        for i in range(N):        if not numList[i] in box:            box.append(numList[i])            backTracking(depth + 1)            box.pop()backTracking(0) 앞선 N과 M ..

[Baekjoon 15652 / Python / 실버3] N과 M (4)

import sysinput = sys.stdin.readline# 1 ~ N 까지의 수, M자리N, M = map(int, input().split())def compare(a, b):    if a > b:        return False    return Truedef backTracking(depth, num):    if depth == M:        print(' '.join(num))        return        for i in range(1, N + 1):        if depth == 0 or compare(int(num[-1]), i):            backTracking(depth + 1, num + str(i))backTracking(0, '') 앞선 N과..

[Baekjoon 15651 / Python / 실버3] N과 M (3)

import sysinput = sys.stdin.readline# 1 ~ N 까지의 수 / 길이는 MN, M = map(int, input().split())def backTracking(depth, num):        if depth == M:        print(' '.join(num))        return        for i in range(1, N + 1):        backTracking(depth + 1, num + str(i))backTracking(0, '') 백트래킹이라고 부르기도 좀 민망하긴 하다. 이전 N과 M 문제를 풀면서 올라왔다면 매우 쉽게 풀 것이다. 그냥 1 ~ N 까지의 수, M 길이를 가지는 모든 수열을 출력하면 된다.visited를 사용할 필요도 없..

[Baekjoon 15650 / Python / 실버3] N과 M (2)

# 우선 모든 경우의 수 수열을 만드는 코드를 짠다# depth가 0일 땐 바로 재귀 돌린다# depth가 0이 아니라면 "이전 숫자와 비교해 더 클 경우" 재귀 돌린다# num은 str 형태로 전달되며, 비교할 때만 int로 변환해 비교한다# 수열이 완성되면 str형태의 num을 중간에 공백을 추가해서 result 리스트에 담는다# result 리스트의 완성된 수열들을 차례로 출력한다import sysinput = sys.stdin.readline# 1 ~ N 까지의 수 / M개를 고른다N, M = map(int, input().split())visited = [False] * (N + 1) # 수와 인덱스를 맞추기 위해 + 1 한다result = []# 조건 검사 함수def compare(a, b):..

728x90
반응형