본문 바로가기

Algorithm

(203)
[BOJ] 4949번 균형잡힌 세상 (C++) https://www.acmicpc.net/problem/4949 4949번: 균형잡힌 세상 하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 각 줄은 마침표(".")로 끝난다 www.acmicpc.net #include #include #include using namespace std; int main() { while(true){ string str; getline(cin, str); if(str == ".") break; stack st; string ans = "yes"; for(int i = 0; i < str.length(); i++){ if(str[i] == '(..
[BOJ] 2805번 나무 자르기 (C++) https://www.acmicpc.net/problem/2805 2805번: 나무 자르기 첫째 줄에 나무의 수 N과 상근이가 집으로 가져가려고 하는 나무의 길이 M이 주어진다. (1 ≤ N ≤ 1,000,000, 1 ≤ M ≤ 2,000,000,000) 둘째 줄에는 나무의 높이가 주어진다. 나무의 높이의 합은 항상 M보 www.acmicpc.net #include #include using namespace std; int main() { long long n, m; cin >> n >> m; vector v(n); long long min = 0, max = 0; long long result = 0; for(int i = 0; i > v[i]; if(v[i] > max)..
[BOJ] 16173번 점프왕 쩰리 (Small) (C++) https://www.acmicpc.net/problem/16173 16173번: 점프왕 쩰리 (Small) 쩰리는 맨 왼쪽 위의 칸에서 출발해 (행, 열)로 나타낸 좌표계로, (1, 1) -> (2, 1) -> (3, 1) -> (3, 3)으로 이동해 게임에서 승리할 수 있다. www.acmicpc.net #include #include using namespace std; int n; int arr[3][3]; string bfs(){ queue q; q.push({0, 0}); while(!q.empty()){ int x = q.front().first; int y = q.front().second; int step = arr[x][y]; q.pop(); if(step == -1) return "H..
[Programmers] 로또의 최고 순위와 최저 순위 (C++) https://school.programmers.co.kr/learn/courses/30/lessons/77484 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr #include #include #include using namespace std; vector solution(vector lottos, vector win_nums) { vector answer; int cnt1 = 0, cnt2 = 0; for(int i = 0; i < 6; i++){ if(find(win_nums.begin(), win_nums.end(), lottos[i]) != wi..
[BOJ] 2960번 에라토스테네스의 체 (C++) https://www.acmicpc.net/problem/2960 2960번: 에라토스테네스의 체 2, 4, 6, 8, 10, 3, 9, 5, 7 순서대로 지워진다. 7번째 지워진 수는 9이다. www.acmicpc.net #include using namespace std; int main() { int num[1001]; int n, k; int cnt = 0; cin >> n >> k; for(int i = 2; i
[C++] 16935번 배열 돌리기 3 (C++) https://www.acmicpc.net/problem/16935 16935번: 배열 돌리기 3 크기가 N×M인 배열이 있을 때, 배열에 연산을 R번 적용하려고 한다. 연산은 총 6가지가 있다. 1번 연산은 배열을 상하 반전시키는 연산이다. 1 6 2 9 8 4 → 4 2 9 3 1 8 7 2 6 9 8 2 → 9 2 3 6 1 5 1 8 3 4 2 9 → www.acmicpc.net #include using namespace std; int n, m, r; int arr[100][100]; int tmp[100][100]; void one(){ for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ tmp[i][j] = arr[n - 1 - i][j]; ..
[BOJ] 1235번 학생 번호 (C++) https://www.acmicpc.net/problem/1235 1235번: 학생 번호 첫째 줄에는 학생의 수 N(2≤N≤1,000)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 학생의 학생 번호가 순서대로 주어진다. 모든 학생들의 학생 번호는 서로 다르지만 그 길이는 모두 같으며, 0부 www.acmicpc.net #include #include #include using namespace std; int main() { int n; cin >> n; string s[1000]; for(int i = 0; i > s[i]; } int ans = s[0].size(); for(int i = 0; i < s[0].size(); i++){ set st; for(int j = ..
[BOJ] 17352번 여러분의 다리가 되어드리겠습니다! (C++) https://www.acmicpc.net/problem/17352 b) parent[a] = b; else parent[b] = a; } bool findParent(int a, int b){ a = getParent(a); b = getParent(b); if(a == b) return true; else return false; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i = 1; i > a >> b; unionParent(a, b); } for(int i = 1; i