Algorithm/Baekjoon
-
[Python][C++] 1074번 ZAlgorithm/Baekjoon 2024. 7. 4. 17:49
문제https://www.acmicpc.net/problem/1074 코드1. Pythondef recursion(n, x, y): global ans # 재귀의 종료 조건, n이 0이 될 경우 위치 (x, y)에 도달했음을 의미 if n == 0: ans += x * 2 + y print(ans) return # 첫 번째 사분면: 좌상단 (x, y가 모두 2^n보다 작은 경우) if x = 2 ** n: ans += 2 ** (2 * n) # 해당 사분면 전에 탐색해야 하는 전체 항목 수 추가 recursion(n - 1, x, y - 2 ** n) # y 좌표 조정하여 재귀 호출 # 세 번째 사분면..
-
[Python][C++] 1012번 유기농 배추Algorithm/Baekjoon 2024. 7. 2. 17:40
문제https://www.acmicpc.net/problem/1012 코드1. Pythonimport sysinput = sys.stdin.readline # 입력을 빠르게 받기 위해 sys.stdin.readline 사용sys.setrecursionlimit(10**6) # 재귀 깊이 한계 설정, 대규모 그리드 처리를 위함dx = [-1, 1, 0, 0] # x축 이동 방향 (상, 하)dy = [0, 0, -1, 1] # y축 이동 방향 (좌, 우)def dfs(x, y): # 그리드 범위를 벗어나는 경우 즉시 종료 if x = n or y = m: return False # 현재 위치에 1이 있으면, 해당 위치를 0으로 바꾸고 주변을 탐색 if graph..
-
[Python][C++] 1003번 피보나치 함수Algorithm/Baekjoon 2024. 7. 2. 15:58
문제https://www.acmicpc.net/problem/1003 코드1. Pythont = int(input())dp = [[0, 0] for _ in range(41)]dp[0] = [1, 0]dp[1] = [0, 1]for i in range(2, 41): dp[i][0] = dp[i-1][0] + dp[i-2][0] dp[i][1] = dp[i-1][1] + dp[i-2][1] for _ in range(t): n = int(input()) print(*dp[n]) 2. C++#include #include using namespace std;int main() { ios_base::sync_with_stdio(false); cin.tie(nu..
-
[Python][C++] 11866번 요세푸스 문제 0Algorithm/Baekjoon 2024. 7. 1. 01:22
문제https://www.acmicpc.net/problem/11866 코드1. Pythonfrom collections import dequedef josephus(N, K): q = deque(range(1, N + 1)) result = [] while q: q.rotate(-(K-1)) # K-1만큼 왼쪽으로 회전 (맨 앞의 요소가 K번째 요소가 되도록) result.append(q.popleft()) # 회전 후 맨 앞의 요소를 제거하고 결과 리스트에 추가 return resultN, K = map(int, input().split())ans = josephus(N, K)print("") 2. C++#include #include #in..
-
[파이썬][C++] 11651번 좌표 정렬하기 2Algorithm/Baekjoon 2024. 6. 30. 23:46
문제https://www.acmicpc.net/problem/11651 코드1. Pythonimport sysinput = sys.stdin.readlinen = int(input())points = [list(map(int, input().split())) for _ in range(n)]points.sort(key=lambda x: (x[1], x[0]))for p in points: print(*p) 2. C++#include #include #include using namespace std;bool compare(const pair& a, const pair& b) { if (a.second == b.second) { return a.first > n; vector> points; ..
-
[Python][C++] 11650번 좌표 정렬하기Algorithm/Baekjoon 2024. 6. 30. 23:26
문제https://www.acmicpc.net/problem/11650 코드1. Pythonn = int(input())points = [list(map(int, input().split())) for _ in range(n)]points.sort()for p in points: print(*p) 2. C++#include #include #include using namespace std;int main() { int n; cin >> n; vector> arr; int x, y; while (n--) { cin >> x >> y; arr.emplace_back(x, y); } sort(arr.begin(), arr.end()); for (const auto& a: arr) cout
-
[Python][C++] 10845번 큐Algorithm/Baekjoon 2024. 6. 29. 15:25
문제https://www.acmicpc.net/problem/10845 코드1. Pythonimport sysinput = sys.stdin.readlinen = int(input())queue = []for i in range(n): cmd = input().split() if cmd[0] == "push": queue.insert(0, cmd[1]) elif cmd[0] == "pop": if len(queue): print(queue.pop()) else: print(-1) elif cmd[0] == "size": print(len(queue)) elif cmd[0] == "empty": i..
-
[Python][C++] 10828번 스택Algorithm/Baekjoon 2024. 6. 29. 14:44
문제https://www.acmicpc.net/problem/10828 코드1. Pythonimport sysinput = sys.stdin.readlinen = int(input())arr = []for _ in range(n): cmd = input().rstrip() if cmd[:4] == "push": cmd, i = cmd.split() arr.append(i) elif cmd == "pop": if len(arr): print(arr.pop()) else: print(-1) elif cmd == "size": print(len(arr)) elif cmd == ..