백준

    [코딩테스트] 백준 촌수계산 파이썬(python)

    https://www.acmicpc.net/problem/2644 2644번: 촌수계산 사람들은 1, 2, 3, …, n (1 ≤ n ≤ 100)의 연속된 번호로 각각 표시된다. 입력 파일의 첫째 줄에는 전체 사람의 수 n이 주어지고, 둘째 줄에는 촌수를 계산해야 하는 서로 다른 두 사람의 번호가 주어 www.acmicpc.net n = int(input()) start, target = map(int, input().split()) m = int(input()) graph = {i : [] for i in range(1, n+1)} for _ in range(m): p, c = map(int, input().split()) graph[p].append(c) graph[c].append(p) visite..

    [코딩테스트] 백준 수 찾기 파이썬(Python)

    https://www.acmicpc.net/problem/1920 1920번: 수 찾기 첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들 www.acmicpc.net 성공코드에 대하여 이야기하기전에 시간초과로 실패한 코드부터 이야기하겠습니다. 실패코드 N = int(input()) A = list(map(int, input().split())) M = int(input()) numbers = list(map(int, input().split())) results = [] for number in numbers: ..

    [코딩테스트] 백준 부분수열의 합 파이썬(Python)

    https://www.acmicpc.net/problem/1182 1182번: 부분수열의 합 첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다. www.acmicpc.net from itertools import combinations n, s = map(int, input().split()) array = list(map(int, input().split())) result = 0 for i in range(1, n+1): for numbers in combinations(array, i): if sum(numbers) == s:..

    [코딩테스트] 백준 N과 M (2) 파이썬(Python)

    https://www.acmicpc.net/problem/15650 15650번: N과 M (2) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net n,m = map(int, input().split()) result = [] k = 1 def solve(k): if len(result) == m: print(' '.join(map(str, result))) return for i in range(k, n+1): if i not in result: result.append(i) solve(i+1) result.pop() solve(k) N과..

    [코딩테스트] 백준 N과 M (1) 파이썬(Python)

    https://www.acmicpc.net/problem/15649 15649번: N과 M (1) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.acmicpc.net n, m = map(int, input().split()) result = [] def solve(): if len(result) == m: print(' '.join(map(str, result))) return for i in range(1, n+1): if i not in result: result.append(i) solve() result.pop() solve() 저는 두가지 방법을 이..