본문 바로가기

python algorithm

(4)
백준 1158: 오세푸스 문제 https://www.acmicpc.net/problem/1158   해설 (queue를 이용한 원순열 구현)from collections import dequequeue = deque()count = 1n, k = map(int, input().split())ans = []for i in range(n): queue.append(i + 1)while queue: if count != k: queue.append(queue.popleft()) count += 1 elif count == k: ans.append(queue.popleft()) count = 1print('', end='')
백준 1874번: 스택 수열 https://www.acmicpc.net/problem/1874해설"""1 2 3 4 (PUSH) STACK:1 2 (POP) STACK: 4 31 2 5 6 (PUSH) STACK:1 2 5 (POP) STACK: 4 3 61 2 5 7 8 (PUSH) STACK:(POP) STACK: 4 3 6 8 7 5 2 1"""count = 1 n = int(input()) stack = []ans = []define = Truefor _ in range(n): num = int(input()) while count
python 함수 모음 📌입출력input()자동으로 줄바꿈 문자를 제거, 프롬프트 문자열을 출력 가능, 속도 느림. sys.stdin.readline()줄바꿈 문자를 제거하지 않고 그대로 반환(split() 이용), 속도 빠름. print()ex) print("hello world", end='\n'), 출력후 새 줄로 넘김 📌 문자열.split()문자열을 지정한 구분자(default: 공백)를 기준으로 여러개의 문자열로 나눔.더보기기본분할: text = "hello world"parts = text.split()print(parts) # ['hello', 'world'] 구분자 지정:text = "apple,banana,cherry"parts = text.split(',')print(parts) # ['apple', ..
백준 10828번: 스택 https://www.acmicpc.net/problem/10828💡문제정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.명령은 총 다섯 가지이다.push X: 정수 X를 스택에 넣는 연산이다.pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.size: 스택에 들어있는 정수의 개수를 출력한다.empty: 스택이 비어있으면 1, 아니면 0을 출력한다.top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.입력첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다...