Algorithm/Baekjoon
-
[파이썬/Python] 2164번 카드2Algorithm/Baekjoon 2024. 5. 29. 15:22
문제https://www.acmicpc.net/problem/2164 코드from collections import dequen = int(input())queue = deque([i for i in range(1, n+1)])# 하나의 요소가 남을 때까지 반복while n > 1: queue.popleft() # 덱의 가장 앞에 있는 요소를 제거 queue.append(queue.popleft()) # 다음 요소를 덱의 뒤로 옮김 n -= 1print(queue[0]) 풀이이 문제는 n명이 원을 이루어 앉고, 특정 규칙에 따라 사람을 제거해 가면서 마지막으로 남는 사람을 찾는 요세푸스 문제와 같다. https://www.acmicpc.net/problem/1158 # 하나의 요소..
-
[파이썬/Python] 2108번 통계학Algorithm/Baekjoon 2024. 5. 27. 15:06
문제https://www.acmicpc.net/problem/2108 코드import sysfrom collections import Counterinput = sys.stdin.readlinen = int(input())arr = []for _ in range(n): arr.append(int(input()))arr.sort() # 리스트를 오름차순으로 정렬mode = Counter(arr).most_common() # 리스트의 각 요소의 빈도수를 계산하고, 빈도수가 높은 순으로 정렬# 평균을 계산하고, 반올림print(round(sum(arr)/n))# 중앙값print(arr[n//2])# 최빈값if len(arr) > 1: # 리스트의 요소가 2개 이상인 경우 if mode[0][..
-
[파이썬/Python] 1966번 프린터 큐Algorithm/Baekjoon 2024. 5. 13. 15:07
문제https://www.acmicpc.net/problem/1966 코드t = int(input())for _ in range(t): n, m = map(int, input().split()) imp = list(map(int, input().split())) idx = list(range(len(imp))) idx[m] = 'target' # 인쇄된 문서의 순서 기록 order = 0 while imp: # 현재 대기목록의 첫 문서가 가장 중요도가 높은 경우 인쇄 진행 if imp[0] == max(imp): order += 1 # 첫 문서가 타겟 문서인 경우 그 순서를 출력하고 반복 종료 ..
-
[파이썬/Python] 1929번 소수 구하기Algorithm/Baekjoon 2024. 5. 13. 15:01
문제https://www.acmicpc.net/problem/1929 코드import mathm, n = map(int, input().split())for i in range(m, n + 1): if i == 1: # 1은 소수가 아니므로 제외 continue flag = 0 # i가 소수인지 아닌지 # 2부터 i의 제곱근까지 for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: # i가 j로 나누어떨어지면 i는 소수가 아님 flag = 1 break if flag == 0: # flag가 0이면 i는 소수 print(i) 풀이n의 약수는 항..
-
[파이썬/Python] 1920번 수 찾기Algorithm/Baekjoon 2024. 5. 2. 18:19
문제https://www.acmicpc.net/problem/1920 코드n = int(input())nums = list(map(int, input().split()))m = int(input())targets = list(map(int, input().split()))# 이진 탐색def binary_search(t, arr, left, right): if left > right: return 0 mid = (left + right) // 2 if t == arr[mid]: return 1 # 만약 대상 원소가 중간 인덱스의 원소보다 작다면, 왼쪽 부분 배열에서 검색. elif t 풀이이 문제는 배열안에 주어진 수가 존재하는 지 찾는 문..
-
[파이썬/Python] 1874번 스택 수열Algorithm/Baekjoon 2024. 4. 30. 16:56
문제https://www.acmicpc.net/problem/1874 코드n = int(input()) # 입력 받을 수의 개수sequence = [int(input()) for _ in range(n)] # 수열 입력 받기stack = []result = []current = 1possible = Truefor num in sequence: while current 풀이1. 변수 초기화stack = []result = [] # +, - 결과 담을 리스트current = 1 # 현재 숫자possible = True 2. 로직문제의 기본 로직은 다음과 같다.1부터 n까지의 숫자를 차례대로 스택에 push스택의 top이 입력받은 수열의 현재 숫자와 같다면 pop이 과정을 모든 수에 대해 반복..
-
[파이썬/Python] 1676번 팩토리얼 0의 개수Algorithm/Baekjoon 2024. 4. 25. 17:05
문제https://www.acmicpc.net/problem/1676 1676번: 팩토리얼 0의 개수N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.www.acmicpc.net 코드n = int(input())cnt = 0factor = 5# n 이하의 모든 5의 배수의 개수를 계산while n >= factor: cnt += n // factor factor *= 5print(cnt) 풀이이 문제는 파이썬으로 세 가지 방법으로 풀 수 있다. 1. factorial 직접 구현n = int(input())factorial = 1for i in range(2, n + 1): factorial *= i cnt = 0for s in reversed(..
-
[파이썬/Python] 1654번 랜선 자르기Algorithm/Baekjoon 2024. 4. 24. 18:07
문제https://www.acmicpc.net/problem/1654 1654번: 랜선 자르기첫째 줄에는 오영식이 이미 가지고 있는 랜선의 개수 K, 그리고 필요한 랜선의 개수 N이 입력된다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이다. 그리고 항상 K ≦ N 이다. 그www.acmicpc.net 코드import sysinput = sys.stdin.readlinek, n = map(int, input().split())cables = [int(input()) for _ in range(k)]# 이진 탐색을 위한 시작과 끝 지점 설정start, end = 1, max(cables)# 이진 탐색 실행while start..