본문 바로가기

Programming [Python]

(411)
#256 백준 파이썬 [15657] N 과 M (8) - 중복조합 https://www.acmicpc.net/problem/15657 Python Code from itertools import combinations_with_replacement #중복 조합 함수 N, M = map(int, input().split()) N_list = list(map(int, input().split())) N_list = sorted(N_list) #순서대로 나오게 정렬 먼저 for numbers in list(combinations_with_replacement(N_list, M)): for num in numbers: print(num, end=' ') print()
#255 백준 파이썬 [15664] N과 M (7) - 중복 순열 https://www.acmicpc.net/problem/15656 Python Code from itertools import product N, M = map(int, input().split()) N_list = list(map(int, input().split())) N_list = sorted(N_list) #순서대로 나오게 정렬 먼저 for numbers in list(product(N_list, repeat = M)): #중복 순열 함수 for num in numbers: print(num, end=' ') print()
#254 백준 파이썬 [15655] N과 M (6) - 조합 https://www.acmicpc.net/problem/15655 Python Code from itertools import combinations N, M = map(int, input().split()) N_list = list(map(int, input().split())) N_list = sorted(N_list) #순서대로 나오게 정렬 먼저 for numbers in list(combinations(N_list, M)): #조합 메써드 이용 for num in numbers: print(num, end=' ') print()
#253 백준 파이썬 [10996] 별 찍기 - 21 https://www.acmicpc.net/problem/10996 Python Code N = int(input()) if N == 1: print('*') else: if N % 2 == 0: a = '* ' * (N//2) b = ' *' * (N//2) else: a = '* ' * (N//2) + '*' b = ' *' * (N//2) for _ in range(N): print(a) print(b)
#252 백준 파이썬 [2167] 2차원 배열의 합 https://www.acmicpc.net/problem/2167 Solution 할 때마다 for 문을 행렬로 두 번 돌려 sum을 구하는 방식은 시간 초과가 난다. O(n^2)이기 때문. 1행 1열부터 x행 y열까지의 sum을 한 번에 구해놓고 sum_matrix 리스트에 저장한 뒤 더하기와 빼기로 구하는 방법을 활용한다. 메인 식은 다음과 같다. print(sum_matrix[x-1][y-1] - sum_matrix[i-2][y-1] - sum_matrix[x-1][j-2] + sum_matrix[i-2][j-2]) if문으로 리스트 범위를 벗어날 경우(i-2, j-2) 예외 처리만 확실하게 해준다. Python Code import copy N, M = map(int, input().split()..
#251 백준 파이썬 [13420] 사칙연산 https://www.acmicpc.net/problem/13420 Solution eval 함수를 통해서 쉽게 풀이할 수 있다. 또한 =을 기준으로 나누어지므로 split함수를 적절하게 이용하면 쉽게 풀 수 있다. Python Code T = int(input()) for _ in range(T): prob, answer = map(str, input().split('=')) if eval(prob) == int(answer): print("correct") else: print("wrong answer")
#250 백준 파이썬 [15654] N과 M (5) - 순열 https://www.acmicpc.net/problem/15654 Python Code from itertools import permutations N, M = map(int, input().split()) N_list = list(map(int, input().split())) N_list = sorted(N_list) #순서대로 나오게 정렬 먼저 for numbers in list(permutations(N_list, M)): for num in numbers: print(num, end=' ') print()
#249 백준 파이썬 [16194] 카드 구매하기 2 https://www.acmicpc.net/problem/16194 Python Code https://claude-u.tistory.com/279와 같은 방식으로 풀 수 있다. max함수만 min함수로 바꾸면 된다. N = int(input()) card = [0] card += list(map(int, input().split())) dp = [0] * (N+1) dp[1] = card[1] dp[2] = min(card[2], card[1]*2) for i in range(3, N+1): dp[i] = card[i] #자기 자신으로 만드는 경우 for j in range(1, i//2 + 1): #j와 i-j로 만드는 경우 dp[i] = min(dp[i], dp[j] + dp[i-j]) print(..