Algorithm/Baekjoon
-
[Python][C++] 10816번 숫자 카드 2Algorithm/Baekjoon 2024. 6. 28. 21:12
문제https://www.acmicpc.net/problem/10816 코드1. Pythonfrom collections import defaultdictn = int(input())cards = list(map(int, input().split()))m = int(input())query = [*map(int, input().split())]card_count = defaultdict(int)for card in cards: card_count[card] += 1for q in query: print(card_count[q], end=' ') 2. C++#include #include using namespace std;int main() { ios::sync_with_stdio(fal..
-
[Python][C++] 10814번 나이순 정렬Algorithm/Baekjoon 2024. 6. 27. 16:28
문제https://www.acmicpc.net/problem/10814 코드1. Pythonn = int(input())people = []for _ in range(n): people.append(tuple(input().split()))people.sort(key = lambda x: int(x[0]))for i in people: print(i[0], i[1]) 2. C++#include #include #include using namespace std;bool compare(const pair& a, const pair& b) { return a.first > n; vector> arr; int age; string name; while (n--) { cin >> age >> na..
-
[Python][C++] 10773번 제로Algorithm/Baekjoon 2024. 6. 27. 13:50
문제https://www.acmicpc.net/problem/10773 코드1. Pythonimport sysinput = sys.stdin.readlinek = int(input())arr = []for _ in range(k): n = int(input()) if n: arr.append(n) else: arr.pop()print(sum(arr)) 2. C++#include #include using namespace std;int main() { int k; cin >> k; stack s; int temp; for (int i = 0; i > temp; if (temp == 0) s.pop();..
-
[파이썬/Python] 9012번 괄호Algorithm/Baekjoon 2024. 6. 13. 14:51
문제https://www.acmicpc.net/problem/9012 코드for _ in range(int(input())): s = input() stk = [] is_valid = True for char in s: if char == '(': stk.append(char) elif char == ')': if not stk or stk[-1] != '(': is_valid = False break stk.pop() if is_valid and not stk: print('YES') else: print('..
-
[파이썬/Python] 7568번 덩치Algorithm/Baekjoon 2024. 6. 11. 17:26
문제https://www.acmicpc.net/problem/7568 코드import sysinput = sys.stdin.readline# 입력받을 쌍의 개수n = int(input())# 쌍의 정수를 저장할 리스트 초기화data = []# 순위를 저장할 리스트 초기화ans = []# n개의 쌍을 입력받아 data 리스트에 저장for i in range(n): a, b = map(int, input().split()) data.append((a, b))# 각 쌍에 대해 다른 모든 쌍과 비교하여 순위를 매김for i in range(n): count = 0 for j in range(n): # (a, b)가 (c, d)보다 두 값 모두 작을 경우 카운트 증가 ..
-
[파이썬/Python] 4949번 균형잡힌 세상Algorithm/Baekjoon 2024. 6. 11. 16:10
문제https://www.acmicpc.net/problem/4949 코드import sysinput = sys.stdin.readlinewhile True: s = input().rstrip() if s == '.': break stack = [] # 괄호를 저장할 스택 balanced = True # 문자열의 괄호가 균형 잡혀 있는지 여부 # 문자열의 각 문자를 순회 for char in s: if char in '([': # 여는 괄호는 스택에 추가 stack.append(char) elif char == ')': # 닫는 소괄호를 만난 경우 if not stack or stack[..
-
[파이썬/Python] 2839번 설탕 배달Algorithm/Baekjoon 2024. 6. 10. 18:16
문제https://www.acmicpc.net/problem/2839 코드n = int(input())# 배달 가능한 봉지의 개수를 저장할 변수 초기화bg = 0while n >= 0: if n % 5 == 0: # 5kg 봉지로 나눌 수 있는 수를 bg에 더함 bg += n // 5 # 최종 봉지 개수를 출력하고 반복문을 종료 print(bg) break # n이 5로 나누어 떨어지지 않는다면 3kg 봉지 하나를 사용하고 n에서 3을 뺌 n -= 3 # 봉지 개수를 하나 증가 bg += 1# n이 음수가 되면 원하는 봉지 조합을 만들 수 없으므로 -1else: print(-1) 풀이기본적인 아이디어는 그리..
-
[파이썬/Python] 2751번 수 정렬하기 2Algorithm/Baekjoon 2024. 6. 3. 10:26
문제https://www.acmicpc.net/problem/2751 코드import sysinput = sys.stdin.readlinen = int(input())arr = [int(input()) for _ in range(n)] arr.sort()for i in arr: print(i) 풀이파이썬 내장 함수 sort()나 sorted()를 사용하면 쉽게 풀 수 있다.직접 정렬을 구현하고 싶다면 병합 정렬로 풀면 된다. 파이썬 내장 함수 참고https://ehdbs0903.tistory.com/entry/파이썬의-정렬-알고리즘은-어떤-알고리즘을-사용할까 파이썬의 정렬 알고리즘은 어떤 알고리즘을 사용할까?백준 문제를 풀다가 문득 궁금한 점이 생겼다.많은 정렬 알고리즘이 있는데, lis..