본문 바로가기
  • Let's study

C++54

[알고리즘] 선택 정렬 - C++ 선택 정렬 #include using namespace std; int arr[] = { 4, 2, 6, 1, 5, 3 }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); int idx = 0; for (int i = 0; i < 6; i++) { int min = 100;//배열의 값보다 큰 값으로 잡기 for (int j = i; j < 6; j++) { if (arr[j] < min) {//제일 작은 값 찾기 min = arr[j]; idx = j;//제일 작은 값이 저장되어 있는 인덱스 } } int temp = arr[i];//i와 제일 작은 값이 저장된 인덱스와 자리 바꾸기 arr[i] = arr[id.. 2021. 9. 22.
[알고리즘] 버블 정렬 - C++ 버블 정렬 #include using namespace std; int arr[] = {4, 2, 6, 1, 5, 3}; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); for (int i = 0; i arr[j + 1]){//붙어있는 두 값을 비교하여 왼쪽보다 오른쪽이 작을 경우 int temp = arr[j];//자리 바꿈 arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for (int i=0; i < 6; i++) cout 2021. 9. 22.
[알고리즘] Algorithm 헤더 - C++ min / max min(a,b): a와 b 중에 제일 작은 수를 반환 max(a,b): a와 b 중에 제일 큰 수를 반환 원래는 (a,b) 안에 2개의 값만 넣을 수 있지만 ({a,b,c,d}) 형식으로 사용하면 여러 값의 크기를 비교할 수 있다. #include #include using namespace std; int main(){ cout 2021. 9. 19.
[C++] 백준 2225번: 합분해 dp라는 건 금방 감이 왔고 N과 K가 만큼 더해야 하니까 2차원 배열을 써야겠다고 생각이 났다. dp 배열에서 행은 N, 열은 K를 저장한다고 볼 때 dp[3][3]을 구해보자. dp[1][1]=1, dp[1][2]=2, dp[1][3]=1; dp[2][1]=1, dp[2][2]=3, dp[2][3]=4; dp[3][1]=1, dp[3][2]=4, dp[3][3]=8; dp[2][2]=dp[1][1]+dp[1][2]이고 dp[3][3]=dp[2][1]+dp[2][2]+dp[2][3] 임을 볼 수 있다. #include #define MAX 1000000000 using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0), c.. 2021. 9. 12.
[C++] 백준 2468번: 안전 영역 #include #include #include #include using namespace std; int arr[101][101]; bool visit[101][101]; int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 }; int n, height[101]; void dfs(int x, int y, int h) { visit[x][y] = true; for (int i = 0; i = 0 && nx = 0 && ny h) { dfs(nx, ny, h); } } } int main() { .. 2021. 9. 12.
[C++] 백준 20046번: Road Reconstruction #include #include #include #define MAX 1e9 using namespace std; int m, n; int dis[1001][1001]; int adj[1001][1001]; int dx[4] = { 1,-1,0,0 }, dy[4] = { 0,0,1,-1 }; int visit[1001][1001]; void dijkstra(int a, int b) { fill(&dis[0][0], &dis[1000][1001], MAX); priority_queue pq; if (adj[a][b] != -1) { dis[a][b] = adj[a][b]; pq.push({ -dis[a][b], {a,b} }); } while (!pq.empty()) { int c = -pq.top().f.. 2021. 9. 9.