본문 바로가기

전체 글

(131)
93. 최대 부분 증가수열 (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include using namespace std; int a[1000]; int dy[1000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m = 0, res = 0; cin >> n; for(int i = 1; i > a[i]; } dy[1] = 1; for(int i = 2; i = 1; j--) { if(a[j] max) max = dy[j]; } dy[i] = max + 1; if(dy[i] > res) res = dy[i]; } cout
92. 네트워크 선 자르기(동적계획법_ Top-Down : 재귀, 메모이제이션) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include using namespace std; int dy[50]; int DFS(int x) { if(x == 1) { dy[x] = 1; return dy[x]; } if(x == 2) { dy[x] = 2; return dy[x]; } else { if(dy[x] != 0) { return dy[x]; } else { return dy[x] = DFS(x-2) + DFS(x-1); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; DFS(n); cout
91. 네트워크 선 자르기(동적계획법_Bottom-Up 기법) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★☆ #include using namespace std; int dy[50]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; dy[1] = 1; dy[2] = 2; for(int i = 3; i
입출력 속도 / 컴파일러 향상시키기 지금까지 나는 C 의 입출력 형태인 printf() 와 scanf() 를 사용했었다. 근데 이것보다 C++의 cin, cout 으로 더 빠르게 입/출력을 할 수 있다는 것을 알게 되었다. 위와 같은 형태로는 printf와 scanf가 더 빠르다. 그러나 아래 코드를 추가하면 cin, cout이 더 빨라진다. ios_base::sync_wihe_stdio(false); cin.tie(NULL); 이는 C 의 입출력 형태인 printf() 와 scanf() 와 C++의 cin, cout 의 버퍼 동기화를 false 하겠다는 의미이다. 즉 입출력시에 둘 중에 C++의 cin, cout만 사용하겠다는 의미이다. cin.tie(null); 코드는 cin과 cout의 묶음을 풀어준다. 기본적으로 cin과 cout은..
90. 라이언 킹 심바(삼성 SW역량평가 기출 : BFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★★ #include #include using namespace std; int a[26][26]; int ch[26][26]; struct Pos { int x; int y; int dis; Pos(int x, int y, int z) { this -> x = x; this -> y = y; this -> dis = z; } bool operator a.y; else return x > a.x; } else return dis > a.dis; } }; struct Simba { int x; int y; int size; int cnt; void SizeUp() { cnt = 0; size++; } }; int main() { int n, res = 0; scanf("%d", &n); int dx[4] = ..
89. 토마토(BFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★★ #include #include using namespace std; int a[1000][1000]; int dis[1000][1000]; struct Tom { int x; int y; Tom(int x, int y) { this -> x = x; this -> y = y; } }; int main() { int m, n, res = 0; bool flag = true; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; scanf("%d %d", &m, &n); // m과 n을 읽는 순서를 조심하자!!! queue Q; for(int i = 1; i
87. 섬나라 아일랜드(BFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★☆☆ #include #include using namespace std; int n, cnt = 0; struct Pos { int x; int y; Pos(int x, int y) { this -> x = x; this -> y = y; } }; int main() { scanf("%d", &n); int a[n+2][n+2]; // 외곽 라인은 전부 0으로 만들기 위해 int dx[8] = {0, 1, 1, 1, 0 ,-1 , -1, -1}; // x좌표의 상하좌우, 대각선 int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; // y좌표의 상하좌우, 대각선 for(int i = 1; i
86. 피자 배달 거리(삼성 SW역량평가 기출문제 : DFS 활용) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★★★★ #include #include #include using namespace std; int n, m, res = 2147000000, sum = 0; int a[50][50]; int ch[50]; vector house; vector pizza; void DFS(int s, int L) { if(L == m) // 피자집 m개를 뽑았으면 { sum = 0; for(int i = 0; i < house.size(); i++) { int x1 = house[i].first; // 피자집과의 거리를 비교할 집의 x좌표 int y1 = house[i].second; // 피자집과의 거리를 비교할 집의 y좌표 int dis = 2147000000; // 비교할 거리 for(int j = 0; j < m; j+..