[백준] 10814번: 나이순 정렬
·
PS
문제https://www.acmicpc.net/problem/10814  개념정렬  실행 결과     구현 코드(c++)#include #include #include using namespace std;// 정렬 기준 - 나이 오름차순bool comp(const pair& a, const pair& b) { return a.first > n; vector> arr(n); for (int i = 0; i > arr[i].first >> arr[i].second; } // 안정적인 정렬 사용 stable_sort(arr.begin(), arr.end(), comp); for (const auto& member : arr) { cout     코드 설명먼저 나는 일반 sort 함수를 사용했..
[백준] 9935번: 문자열 폭발
·
PS
문제https://www.acmicpc.net/problem/9935  개념문자열, 스택  실행 결과    구현 코드(c++)#include #include using namespace std;int main() { string a, b, res; cin >> a >> b; for (char c : a) { res += c; if (res.size() >= b.size()) { int idx = res.size() - b.size(); if (res.substr(idx) == b) res.erase(idx); } } cout     코드 설명처음 문제를 풀 때는 스택 자료구조를 사용했다. 문자열의 인덱스를 이용해 한 글자씩 스택에 넣고 폭발 문자열과..
[백준] 2667번: 단지번호붙이기
·
PS
문제https://www.acmicpc.net/problem/2667  개념그래프, DFS, BFS  실행 결과     구현 코드(c++)#include #include #include #include using namespace std;int g[25][25];bool v[25][25];int dx[4] = {-1, 1, 0, 0};int dy[4] = {0, 0, -1, 1};int n;int dfs(int x, int y) { int cnt = 1; v[x][y] = true; for (int i = 0; i = 0 && ny >= 0 && nx > n; cin.ignore(); for (int i = 0; i danzis; int cnt = 0; for (int i = 0; i   ..
[코드트리] 제곱수의 합으로 나타내기
·
PS
문제 코드트리 | 코딩테스트 준비를 위한 알고리즘 정석국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.www.codetree.ai  개념DP  실행 결과     구현 코드(c++)#include #include #include using namespace std;int sol(int n) { vector dp(n + 1, 5000); dp[0] = 0; for (int i = 1; i > n; cout     코드 설명최소 개수의 제곱수 합으로 표현하기 위한 최소 제곱수의 수를 저장하는 dp 배열을 선언한다. 최소 제곱수를 계산해야하므로 문제에서 최대 범위로 주어진 5000으로 초기화한다. j를 각 제곱수라고 하면, j를 ..
[백준] 1010번: 다리 놓기
·
PS
문제https://www.acmicpc.net/problem/1010  개념수학(조합), 다이나믹 프로그래밍  실행 결과    구현 코드(c++)#include using namespace std;int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; unsigned long long res = 1; for (int i = 0; i     코드 설명단순히 조합 mCn을 계산하는 문제이다.처음에는 공식을 사용하여 단순 계산식을 만들었으나 시간 초과로 실패하거나 메모리 에러가 발생했다..
[백준] Fly me to the Alpha Centauri
·
PS
문제https://www.acmicpc.net/problem/1011  개념수학  실행 결과     구현 코드(c++)#include #include using namespace std;int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { long long x, y; cin >> x >> y; double dist = y - x; // 거리 double dist_sqrt = sqrt(dist); // 거리 제곱근 int dist_round = round(dist_sqrt); // ..
[백준] 10830번: 행렬 제곱
·
PS
문제https://www.acmicpc.net/problem/10830 개념수학, 분할 정복  실행 결과     구현 코드(c++)#include #include using namespace std;const long long MOD = 1000; // 모듈러 연산용 상수struct Matrix { vector> m; int size; // 행렬크기 n으로 초기화 Matrix(int n) : size(n) { m.resize(n, vector(n)); } // 연산자 오버로딩 - 행렬 곱셈 Matrix operator*(const Matrix& mm) { Matrix result(size); for (int i = 0; i 0) { if (exp % 2 == 1) { ..
[백준] 2178번: 미로 탐색
·
PS
문제https://www.acmicpc.net/problem/2178  개념그래프, BFS  실행 결과     구현 코드(c++)#include #include using namespace std;int n, m;int arr[100][100]; // 미로 배열int dist[100][100]; // 최단 거리 배열int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // 미로의 크기 입력 cin >> n >> m; // dx,dy: 상, 우, 하, 좌 방향 벡터 int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; // 미로 입력 받기 (행->렬) for (int i = 0; ..
[코드트리] 행복한 수열의 개수
·
PS
문제 코드트리 | 코딩테스트 준비를 위한 알고리즘 정석국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.www.codetree.ai  개념구현, 격자 안에서 완전탐색   실행 결과     구현 코드(c++)#include using namespace std;int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; int arr[100][100]; // 격자 입력 받기 for (int i = 0; i > arr[i][j]; } } int happy = 0; // 행 검사 for (int i = 0; i = ..
[백준] 1260번: DFS와 BFS
·
PS
문제https://www.acmicpc.net/problem/1260  개념그래프, DFS, BFS  실행 결과     구현 코드(c++)#include #include #include #include using namespace std;vector> graph; // 2차원 벡터로 선언vector visited; // 방문 처리용 배열 선언// 깊이 우선 탐색: 스택 재귀void dfs(int x) { visited[x] = true; cout q; visited[start] = true; // 현재 노드 방문 처리 q.push(start); // 넣기 while (!q.empty()) { int x = q.front(); // 가장 먼저 들어온 원소 확인 q.pop(); // ..
[백준] 1475번: 방 번호
·
PS
문제https://www.acmicpc.net/problem/1475 개념구현  실행 결과     구현 코드(c++)#include #include using namespace std;int main() { int n; // 방번호 cin >> n; int cnt[10] = {0}; // 0부터 9까지의 등장 횟수를 저장할 배열 while (n > 0) { int a = n % 10; cnt[a]++; n /= 10; } // 6과 9가 등장하는 개수는 합쳐서 계산해야 함 int cnt_tmp = (cnt[6] + cnt[9] + 1) / 2; cnt[6] = cnt_tmp; cnt[9] = cnt_tmp; int cnt_max = cnt[0]; for (int i..
[백준] 5585번: 거스름돈
·
PS
문제https://www.acmicpc.net/problem/5585 개념그리디 알고리즘  실행 결과  구현 코드(c++)#include using namespace std;int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; // 타로가 지불할 돈 int change = 1000 - n; // 잔돈 int coins[6] = {500, 100, 50, 10, 5, 1}; // 잔돈 동전 배열 int answer = 0; // 잔돈 매수 for (int i = 0; i     코드 설명아래..