코딩테스트

·Algorithm/백준
요세푸스 문제 0(11866)https://www.acmicpc.net/problem/11866 문제코드queue를 활용한 문제for문을 활용하여 K번째 되는 수마다 추출해서 result 리스트에 넣도록 함.# 요세푸스 문제# 11866from collections import dequeN, K = map(int, input().split())queue = deque()result = []for i in range(1, N+1): queue.append(i)while len(queue) > 0: for j in range(1, K): queue.append(queue.popleft()) # 1, 2, ..., K-1까지는 popleft()로 뽑아서 뒤로 넘기기 result.a..
·Algorithm/백준
큐2(18258)문제코드queue를 활용한 풀이deque 라이브러릴 활용popleft()를 활용해 가장 앞에 수를 빼고, pop()# 큐 2# 18258import sysfrom collections import dequeinput = sys.stdin.readlineN = int(input())queue = deque()for i in range(N): lst = list(input().split()) if lst[0] == 'push': queue.append(lst[1]) elif lst[0] == 'pop': if len(queue) == 0: print(-1) else: print(queue.poplef..
·Algorithm/백준
도키도키 간식드리미https://www.acmicpc.net/problem/12789 문제코드현재 간식 받을 순서인 turn 을 활용차례로 stack에 쌓고, stack의 마지막 부분이 현재 받을 순서라면 popstack에 남아있는 숫자가 있다면 Sad, 없으면 Nice 출력# 도키도키 간식드리미# 12789import sysinput = sys.stdin.readlineN = int(input())num = list(map(int, input().split()))stack = []turn = 1 # 지금 간식 받는 순서for i in num: stack.append(i) while stack and stack[-1] == turn: stack.pop() turn +=..
·Algorithm/백준
균형잡힌 세상(4949)https://www.acmicpc.net/problem/4949 문제코드스택을 활용한 문제']'나 ')'가 나오면 stack내에 '[' , '('에 따라서 pop을 실시stack내 남은게 없다면 yes, 남은게 있으면 no# 균형잡힌 세상# 4949while True : word = input() stack = [] if word == "." : break for i in word : if i == '[' or i == '(' : stack.append(i) elif i == ']' : if len(stack) != 0 and stack[-1] == '[' : ..
·Algorithm/백준
괄호(9012)https://www.acmicpc.net/problem/9012 문제코드스택을 활용한 괄호 처리 문제'('가 들어오면 쌓고, ')'가 들어오면 pop을 활용해 스택 내의 '('를 뽑아내기# 괄호# 9012import sysinput = sys.stdin.readlineT = int(input())for _ in range(T): stack = [] paren = input() for i in paren: if i == '(': stack.append(i) elif i == ')': if stack: stack.pop() else: pri..
potato_pizza
'코딩테스트' 태그의 글 목록 (4 Page)