본문 바로가기

코딩테스트51

[백준] 18111번 - 마인크래프트 (실버 2) 1. 문제 https://www.acmicpc.net/problem/18111 18111번: 마인크래프트 팀 레드시프트는 대회 준비를 하다가 지루해져서 샌드박스 게임인 ‘마인크래프트’를 켰다. 마인크래프트는 1 × 1 × 1(세로, 가로, 높이) 크기의 블록들로 이루어진 3차원 세계에서 자유롭게 www.acmicpc.net 2. 접근 방식 1차 : 전체 블록의 평균(높이)을 구해서 해당 높이까지 블록을 맞춰주는 로직 작성 -> 틀림 2차 : 0~가장 큰 높이의 블록을 기준으로 각 케이스를 계산하여 가장 시간이 빠른 경우 선택 -> 1%에서 틀림 3차 : 위와 동일하지만 블록을 먼저 다 회수하고 설치 진행 -> 시간초과 4차 : 제거할 블록과 쌓을 블록의 개수를 구하고 시간을 나중에 구함 -> 통과 3. .. 2022. 3. 14.
[백준] 1009번 - 분산처리 (브론즈 3) 1. 문제 https://www.acmicpc.net/problem/1009 1009번: 분산처리 입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트 케이스에 대해 정수 a와 b가 주어진다. (1 ≤ a < 100, 1 ≤ b < 1,000,000) www.acmicpc.net 2. 접근 방식 mod연산의 성질을 통해 큰 제곱수를 연산하지 않고 mod 값을 알아냄 빠른 모듈로 거듭제곱법을 적용함 3. 코드 #include #include using namespace std; typedef unsigned long long ll; int func_1009_mod(int a, int b) { vector tmp, tmp2; int *x; ll res = 1; while(b!.. 2022. 3. 10.
[알고리즘] 하노이의 탑 목적 하노이의 탑 알고리즘을 가시화하여 이해를 쉽게함 가시화된 알고리즘을 재귀 함수로 구현함 구현할 함수는 아래 2개임 ( A탑에 위치한 원반 개수는 N) int count_hanoi(int n) : A에서 C까지 N개의 원판을 이동시키는데 필요한 전체 개수 void hanoi(int n, int from, int to) : n번째 원판을 From에서 To로 이동시킴 탑 이동 알고리즘 (Hanoi Function) 하노이 탑의 이동을 재귀로 구현할 땐 아래 이미지의 이동 방식을 알면 됨 N이 2보다 더 큰 경우도 가장 밑에 있는 N번째 원판과 그위의 나머지 원판으로 부분을 나누면, 위와 같은 방식으로 이동이 가능함 hanoi(2, A, B)와 hanoi(2, B, C)의 이동을 직접 나열해보면 아래와 같.. 2022. 3. 9.
[프로그래머스][SQL] 고득점 kit 프로그래머스 SQL 고득점 Kit Select : https://programmers.co.kr/learn/courses/30/parts/17042 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr -- 1. 모든 레코드 조회하기 SELECT * from animal_ins order by animal_id asc -- 2. 역순 정렬하기 SELECT Name, datetime from animal_ins order by animal_id desc -- 3. 아픈 동물 찾기 SELECT animal_id, name from animal_ins where in.. 2022. 3. 5.
[백준] 삼성 SW 역량 테스트 기출 문제 (작성중) 1. URL : https://www.acmicpc.net/workbook/view/1152 문제집: 삼성 SW 역량 테스트 기출 문제 (baekjoon) www.acmicpc.net 2. 문제풀이 구슬 탈출2 (골드1) 2048(Easy) (골드2) 뱀 (골드 5) 시험 감독(브론즈 2) #include #include #include using namespace std; int main() { double n, tmp, b, c, sum=0; double sub_supervisor; cin >> n; vector exam_room; for (int i = 1; i > tmp; exam_room.push_back(tmp); } cin >> b >> c; for (auto exam : exam_room).. 2022. 2. 22.
[백준] 단계별로 풀어보기 > 그리디 알고리즘 (c++) 문제 1. 동전 0 가장 금액이 큰 동전부터 개수를 세면서 k원에서 차감해줌 k가 0이되면 동전의 개수를 출력 #include #include using namespace std; int main() { int n, k, tmp; map m; map::reverse_iterator reiter; cin >> n >> k; for (int i = 0; i > tmp; m[tmp] = 0; } for(reiter = m.rbegin(); reiter != m.rend(); reiter++) { while (reiter->first first]++; k -= reiter->first; } if (k second > 0) { tmp += reiter->second; } } cout.. 2022. 2. 20.
[백준] 단계별로 풀어보기 > 동적 계획법 1 (c++) 문제 1. 피보나치 함수 피보나치 f(n) 이 몇개의 f(0)과 f(1)로 이루어 져 있는지 출력하는 문제 점화식(?) { f(i).first, f(i).second } = { f(i-1).first+f(i-2).first, f(i-1).second+f(i-2).second } 을 찾음 #include #include #include using namespace std; int main() { int test_case; cin >> test_case; vector f; f.push_back({ 1, 0 }); f.push_back({ 0, 1 }); for (int i = 2; i > n[i]; cout 2022. 2. 16.
[프로그래머스][Lv.3][Cpp] 네트워크 1. 문제 https://programmers.co.kr/learn/courses/30/lessons/43162 코딩테스트 연습 - 네트워크 네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있 programmers.co.kr 2. 접근 방식 입력 받은 인접 행렬에 몇개의 그래프가 존재하는지를 묻는 문제 BFS를 수행 후에도 방문하지 않은 노드가 남아 있다면 다른 그래프가 존재한다고 판단함 (방문하지 않은 노드가 없을 때까지 반복하여 탐색) 인접 행렬 형식의 2차원 Vector를 인접리스트 형식으로 변경 후 BFS 수행 3. 코드 #include #include #in.. 2022. 2. 6.
[프로그래머스][Lv.2][Python] 타겟 넘버 1. 문제 (레벨 2) https://programmers.co.kr/learn/courses/30/lessons/43165 코딩테스트 연습 - 타겟 넘버 n개의 음이 아닌 정수들이 있습니다. 이 정수들을 순서를 바꾸지 않고 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 programmers.co.kr 2. 접근 방식 BFS/ DFS 카테고리에 있는 문제지만, 해당 탐색 알고리즘으로 해결할 방법이 떠오르지 않아 완전탐색으로 구현 +, - 부호를 이진수로 표현하여 모든 경우에 대해 연산한 뒤 개수를 카운팅함 통과는 했지만 메모리와 시간에 제한이 있다면 실패할 듯 함 3. 코드 def solution(numbers, .. 2022. 2. 6.
[백준] 단계별로 풀어보기 > DFS와 BFS (c++) 문제 1. DFS와 BFS #include #include #include #include #include using namespace std; void func_1260_dfs_recursion(int n, vector * graph, bool* visited) { visited[n] = true; cout > start_node; vector *graph = new vector[n+1]; bool *visited = new bool[n+1]; memset(visited, 0, sizeof(bool) * (n+1)); for (int i = 0; i > u >> v; graph[u].push_back(v); graph[v].push_back(u); } for (int i .. 2022. 2. 5.
[알고리즘] DFS/ BFS 목적 탐색 알고리즘에서 사용되는 표현 방식을 이해하고 직접 구현 탐색 알고리즘인 DFS(Depth First Search, 깊이 우선 탐색) 알고리즘에 대해 이해하고 직접 구현 탐색 알고리즘인 BFS(Breadth First Search, 너비 우선 탐색) 알고리즘에 대해 이해하고 직접 구현 인접리스트와 인접 행렬 위 와 같은 그래프가 있을 때, 이 그래프를 표현하는 방식은 크게 2가지(인접리스트, 인접행렬)가 있다. 각 표현 방식을 구현해보면 다음 코드와 같다. 1) 간선을 인접리스트로 변환 #include #include #include #include using namespace std; // 만들어진 그래프를 출력 void print_adjacency_list(map graph) { for (au.. 2022. 2. 5.
[백준] 단계별로 풀어보기 > 정렬 (c++) 문제 1. 수 정렬하기 (브론즈 1) vector로 입력 받아 sort로 정렬 #include #include #include using namespace std; int main() { int a, num; vector v; cin >> a; for (int i = 0; i > num; v.push_back(num); } sort(v.begin(), v.end()); for (int i = 0; i > num; v.push_back(num); } sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { cou.. 2022. 1. 30.
[백준] 1011번 - Fly me to the Alpha Centauri (골드 5) 1. 문제 https://www.acmicpc.net/problem/1011 1011번: Fly me to the Alpha Centauri 우현이는 어린 시절, 지구 외의 다른 행성에서도 인류들이 살아갈 수 있는 미래가 오리라 믿었다. 그리고 그가 지구라는 세상에 발을 내려 놓은 지 23년이 지난 지금, 세계 최연소 ASNA 우주 비행 www.acmicpc.net 2. 코드 #include #include using namespace std; typedef unsigned long long ll; int main() { int int_case; ll x, y, res,distance, level; cin >> int_case; for (int i = 0; i .. 2022. 1. 29.
[백준] 단계별로 풀어보기 > 브루트포스 (c++) 문제 1. 블랙잭 (브론즈 2) #include using namespace std; int main(){ int n, m, sum, min, res = 0; cin >> n >> m; int* card = new int[n]; bool is_flag; for (int i = 0; i > card[i]; } sum = 0; min = m; is_flag = true; for (int i = 0; i < n - 1 && is_flag; i++) { for (int j = i + 1; j < n && is_flag; j++) { for (int k = j + 1; k < n && is_flag; k++) { sum = card[i] + card[j] + card[k]; if (.. 2022. 1. 26.
[백준] 15965번 - K번째 소수 (실버 2) 1. 문제 https://www.acmicpc.net/problem/15965 15965번: K번째 소수 자연수 K가 주어진다.(1 ≤ K ≤ 500,000) www.acmicpc.net 2. 코드 #include #include using namespace std; bool *Sieve_of_Eratosthenes(int m) { bool* arr = new bool[m + 1]; memset(arr, 1, sizeof(bool) * (m+1)); arr[0] = false; arr[0] = false; for (int i = 2; i < m + 1; i++) { if (arr[i] == true) { for (int j = i * 2; j < m + 1; j += i) { arr[j] = false;.. 2022. 1. 25.
[백준] 단계별로 풀어보기 > 재귀 (c++) 문제 1. 팩토리얼 #include using namespace std; int func_10872_factorial(int n) { if (n == 0) return 1; else return n * func_10872_factorial(n-1); } int main() { int a; cin >> a; cout > a; cout 2022. 1. 25.
[백준] 단계별로 풀어보기 > 기본 수학 2 (c++) 문제 1. 소수찾기 #include typedef unsigned long long ll; using namespace std; int is_prime_number_custom(ll input) { if (input a; int* arr = new int[a]; for (int i = 0; i > arr[i]; if (is_prime_number_custom(arr[i])) { cnt++; } } cout a >> b; for (int i = a; i h; if (min > h - y) min = h - y; cout > a >> b; if (x.count(a)) x[a]++; else x[a] = 1; i.. 2022. 1. 19.
[백준] 단계별로 풀어보기 > 기본 수학 1 (c++) 문제 1. 손익분기점 #include using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int unit_profit = c - b; if (unit_profit 3 -> 4 ... 그리고 a/b 가 a는 증가, b는 감소 int lv, pos; int a, b; // 계층 구하기 for (int i = 0; i a; cout a >> b >> v; if (v 0) { day++; } } cout a; for (int i = 0; i > h >> w >> n; f += (n % h) -1; if (n % h == 0) { f = h; no.. 2022. 1. 18.
[백준] 단계별로 풀어보기 > 문자열 (c++) 문제 1. 아스키 코드 #include using namespace std; int main() { cin.tie(NULL); cin.sync_with_stdio(false); char a; cin >> a; cout a >> b; for (int i = 0; i < a; i++) { sum += int(b[i]) - int('0'); } cout s; for (int i=0; i < s.size(); i++) { index = int(s[i]) - int('a'); if (alphabets[index] == -1) { alphabets[index] += (i + 1); } } for (int i = 0; i < arr_size; i++) { cout a; for (int i = 0; i < a; i++.. 2022. 1. 15.
[백준] 단계별로 풀어보기 > 함수 (c++) 문제 1. 정수 N개의 합 #include using namespace std; long long sum(vector& a) { long long ans = 0; for (int i = 0; i < a.size(); i++) { ans += a.at(i); } return ans; } 문제 2. 셀프 넘버 #include #include #include using namespace std; int selfnumber(int a) { int digit = to_string(a).size(), tmp = 0, a_copy = a, sum = a; int* arr = new int[digit]; for (int i = 0; i < digit; i++) { tmp = pow(10, (digit - (i + 1).. 2022. 1. 14.