본문 바로가기

CodingTest Exam/[C++] Algorithm Study

(100)
103. 위상정렬(그래프 정렬) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include #include #include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, a, b, x = 0; cin >> n >> m; vector Degree(n+1, 0); vector Work[n+1]; queue Q; for(int i = 1; i > a >> b; Work[a].push_back(b); } for(int i = 1; i
102. 회장뽑기(플로이드-워샬 응용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include #include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, a, b, score = 2147000000, cnt = 0; cin >> n; vector V(n+1, vector(n+1, 100)); vector Scores(n+1, 0); while(true) { cin >> a >> b; if(a == -1 && b == -1) break; V[a][b] = 1; V[b][a] = 1; } for(int k = 1; k
101. 플로이드 워샬 알고리 (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include #include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, a, b, c; cin >> n >> m; vector dis(n+1, vector(n+1, 5000)); for(int i = 1; i a >> b >> c; dis[a][b] = c; } for(int k = 1; k
100. 최대점수 구하기(냅색 알고리즘) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★☆☆☆ #include #include using namespace std; struct Exam { int score; int time; Exam(int x, int y) { this -> score = x; this -> time = y; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, a, b; cin >> n >> m; vector V; vector Scores(m+1, 0); V.push_back(Exam(0, 0)); for(int i = 0; i > a >> b; V.push_back(Exam(a, b)); } for(int i = 1; i = 0; j--) { if(j - exam.t..
99. 동전교환(냅색 알고리즘) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★☆☆☆ #include #include #include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, a; cin >> n; vector C; for(int i = 0; i > a; C.push_back(a); } cin >> m; vector V(m+1, 0); for(int i = 0; i V[j-C[i]] + 1 || V[j] == 0) V[j] = V[j-C[i]] + 1; } } cout
98. 가방문제(냅색 알고리즘) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★☆☆☆ #include #include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, a, b; cin >> n >> m; vector V; vector Bag(m+1, 0); for(int i = 1; i > a >> b; V.push_back(make_pair(a, b)); } for(int i = 0; i < V.size(); i++) { int w = V[i].first; int v = V[i].second; for(int j = 0; j < Bag.size(); j++) { if(j - w < 0) continue; if(Bag[j] < Bag[j-w] + v) Bag[j] = Bag[j-..
97. 알리바바와 40인의 도둑(Top-Down) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include using namespace std; int a[20][20]; int dy[20][20]; int n; void DFS(int x, int y, int sum) { if(x == n) return; if(y == n) return; if(dy[x][y] == 0) { dy[x][y] = sum; } else { if(dy[x][y] > sum) dy[x][y] = sum; } DFS(x+1, y, sum + a[x+1][y]); DFS(x, y+1, sum + a[x][y+1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i = 0; i < n; i++) { for(int j = 0..
96. 알리바바와 40인의 도둑(Bottom-Up) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★☆☆☆ #include #include using namespace std; int dy[20][20]; int a[20][20]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, p1 = 0, p2 = 0; cin >> n; int X[2] = {0, 1}; int Y[2] = {1, 0}; for(int i = 0; i > a[i][j]; } } dy[0][0] = a[0][0]; while(true) { if(p1 >= n || p2 >= n) break; for(int i = 0; i < 2; i++) { if(dy[p1+X[i]][p2+Y[i]] ..