프로그램/코딩테스트28 백준 5397 키로거 import sys input = sys.stdin.readline for _ in range(int(input())) : left_stk = [] right_stk = [] key_press = input().strip() for key in key_press : if key == '' : if right_stk : left_stk.append(right_stk.pop()) elif key == '-' : if left_stk : left_stk.pop() else : left_stk.append(key) left_stk.extend(reversed(right_stk)) print(''.join(left_stk)) 2개의 스택을 이용해서 해결했다. 키가 나오면 반대로 해결했다. -키가 나오면 왼쪽에 있.. 2022. 6. 13. 백준 7785 회사에 있는 사람 import sys input = sys.stdin.readline names = set() for _ in range(int(input())) : name, ent = input().split() if ent == 'enter' : names.add(name) elif name in names : names.remove(name) for name in sorted(names, reverse = True) : print(name) set 자료형을 이용해 해결했다. 각 줄을 2개로 나누어 입력받고 enter이면 set에 추가시키고, 아니라면 그 이름을 제거한다. 그리고 사전 역순으로 출력하는 코드를 작성했다. 2022. 6. 13. 백준 1302 베스트셀러 books = dict() for _ in range(int(input())) : book = input() if book in books : books[book] += 1 else : books[book] = 1 max_value = max(books.values()) top_book = [] for key, value in books.items() : if value == max_value : top_book.append(key) top_book.sort() print(top_book[0]) map 자료형을 이용해 문제를 해결했다. 입력된 내용을 key에 등록하고 value를 하나씩 증가시키는 코드를 구성하고, 최대 값이 여러개일 경우를 대비해 정렬해 abc순으로 첫번째 내용을 출력한다. 2022. 6. 13. 백준 11286 절댓값 힙 import heapq, sys input = sys.stdin.readline min_heap = [] max_heap = [] for _ in range(int(input())) : num = int(input()) if num > 0 : heapq.heappush(min_heap, num) elif num < 0 : heapq.heappush(max_heap, -num) else : if len(min_heap) == 0 : if len(max_heap) == 0 : print(0) else : print(-heapq.heappop(max_heap)) elif len(max_heap) == 0 or min_heap[0] < max_heap[0] : print(heapq.heappop(min_heap.. 2022. 6. 13. 백준 2164 카드2 from collections import deque count = int(input()) card = deque() for _ in range(count) : card.append(_ + 1) while len(card) != 1 : card.popleft() card.append(card.popleft()) print(card.popleft()) 큐에 1부터 N까지의 숫자를 넣어주고, 가장 앞의 숫자를 버리고 그 뒤의 숫자를 버린 것을 다시 맨 뒤에 넣어주는 코드이다. 2022. 6. 13. 백준 9012 괄호 for _ in range(int(input())) : ans = "YES" stk = [] for c in input() : if c == '(' : stk.append('(') else : if len(stk) > 0 : stk.pop() else : ans = "NO" if len(stk) > 0 : ans = "NO" print(ans) 배열을 스택처럼 활용해 (이 들어오면 (을 push해주고 그 이외의 것이 들어오면 pop을 해주도록 했다. 배열이 비었을 경우에 (이 아닌 다른 문자가 들어오면 NO로 출력되도록 했다. 2022. 6. 12. 백준 11866 요세푸스 문제 0 N, K = map(int, input().split()) peoples = [i for i in range(1, N+1)] i = 0 ans = [] for _ in range(N) : i += K - 1 i %= len(peoples) ans.append(peoples.pop(i)) print(f"") 연결 리스트를 이용해서 pop을 합과 동시에 ans라는 배열에 넣어주어 문제를 해결했다. 2022. 6. 12. 백준 기초 (while 문) 10052번 do while이 없다는게 슬프다 while True: a, b = map(int, input().split()) if a == 0 and b == 0: break print(a+b) 10951번 처음으로 좀 어려웠다 문제가 무슨 뜻인지 이해못했음 그냥 무한루프 돌렸는데 런타임떠서 다시보니 거를거 거르란뜻인듯 try catch 인듯 아닌듯 while True: try: a, b = map(int, input().split()) print(a + b) except: break 1110번 생각 안하고 의식의 흐름대로 만듬 일의자리랑 더한거의 일의자리랑.. num = int(input()) temp = num count = 0 while True: newnum = (temp % 10)*10 + (.. 2020. 12. 18. 백준 기초(for 문) 2739번 여담이지만 난 아직도 구구단을 외우기보다는 그때 그때 계산해서 쓰는편이다 그래서 사칙연산이 느림 A = int(input()) for i in range(1, 10): print("{0} * {1} = {2}".format(A, i, i*A)) 10950번 count = int(input()) for i in range(0, count): plusA, plusB = map(int, input().split()) print(plusA + plusB) 8393번 이거 언어 성능 테스트할때 쓰는거 아닌가 count = int(input()) total = 0 for i in range(0, count+1): total += i print(total) 15552번 파이썬에서 빠르게 입력받으려면 sys.. 2020. 12. 18. 백준 기초 (if문) 1330번 두 수 비교하기 elif는 상상도 못했자너 A, B = map(int, input().split()) if A > B: print(">") elif A == B: print("==") else: print(" 2020. 12. 17. 백준 기초 (입출력과 사칙연산) 2557번 Hello World! 출력 문제 항상 언어를 처음 배울땐 이걸 하는거 같다 print("Hello World!") 10718번 이스케이프 시퀸스였나? 그거 문제인듯 print("강한친구 대한육군\n강한친구 대한육군") 10171번 이스케이프 시퀸스 여러개 print("\ /\\") print(" ) ( \')") print("( / )") print(" \\(__)|") 10172번 print 여러개 치기 귀찮아서 한줄에 해버렸다 print("|\\_/|\n|q p| /}\n( 0 )\"\"\"\\\n|\"^\"` |\n||_/=\\\\__|") 1000번 간단한 입/출력 연산문제 파이썬에서 입력은 처음이라 찾아봄 a, b = map(int, input().split()) print(a+b).. 2020. 12. 17. 다시 처음부터 코딩테스트가 뭔가 궁금해졌다. 파이썬 배울겸 처음부터 해보려함 2020. 12. 16. 이전 1 2 다음