Skip to content

Commit 5fd69eb

Browse files
committed
update
1 parent 5cf4b01 commit 5fd69eb

File tree

9 files changed

+382
-0
lines changed

9 files changed

+382
-0
lines changed

lecture1/lecture1/ex072.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
// 공주 구하기 (큐 자료구조로 해결)
8+
9+
void ex072() {
10+
int n = 8; // 왕자수
11+
int c = 3;
12+
queue<int> Q;
13+
14+
for (int i = 1; i <= n; i++) {
15+
Q.push(i); // 1 방문
16+
}
17+
18+
int x;
19+
int count = 1;
20+
while (Q.size() != 1) {// Q가 비어 있으면 종료
21+
x = Q.front(); // 맨 앞 값 참조
22+
Q.pop(); // 맨 앞 꺼내기(지우기)
23+
if (count == c) {
24+
count = 1;
25+
}
26+
else {
27+
Q.push(x);
28+
count++;
29+
}
30+
}
31+
x = Q.front();
32+
cout << x << endl;
33+
}
34+
35+
//int main() {
36+
// ex072();
37+
// cout << endl << "done!" << endl;
38+
//}
39+
//// 7

lecture1/lecture1/ex073.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
// 최대힙 (priority_queue : 우선순위 큐)
8+
9+
//최대힙은 완전이진트리로 구현된 자료구조입니다.그 구성은 부모 노드값이 왼쪽자식과 오른
10+
//쪽 자식노드의 값보다 크게 트리를 구성하는 것입니다.그렇게 하면 트리의 루트(root)노드는
11+
//입력된 값들 중 가장 큰 값이 저장되어 있습니다.
12+
13+
void ex073() {
14+
15+
priority_queue<int> pQ;
16+
vector<int> input = { 5,3,6,0,5,0,2,4,0,-1 };
17+
int a;
18+
int pos = 0;
19+
while (true) {
20+
a = input[pos]; // 맨 앞 값 참조
21+
pos++;
22+
if (a == -1) break;
23+
if (a == 0) {
24+
if (pQ.empty()) {
25+
cout << "-1" << endl;
26+
}
27+
else {
28+
cout << pQ.top() << endl; //root 값
29+
pQ.pop();
30+
}
31+
}
32+
else {
33+
pQ.push(a);
34+
}
35+
}
36+
}
37+
38+
//int main() {
39+
// ex073();
40+
// cout << endl << "done!" << endl;
41+
//}
42+
//6
43+
//5
44+
//5

lecture1/lecture1/ex074.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
// 최소힙 (priority_queue : 우선순위 큐)
8+
9+
//최소힙은 완전이진트리로 구현된 자료구조입니다.그 구성은 부모 노드값이 왼쪽자식과 오른
10+
//쪽 자식노드의 값보다 작게 트리를 구성하는 것입니다.그렇게 하면 트리의 루트(root)노드는
11+
//입력된 값들 중 가장 작은 값이 저장되어 있습니다.
12+
13+
void ex074() {
14+
15+
priority_queue<int> pQ;
16+
vector<int> input = { 5,3,6,0,5,0,2,4,0,-1 };
17+
int a;
18+
int pos = 0;
19+
while (true) {
20+
a = input[pos]; // 맨 앞 값 참조
21+
pos++;
22+
if (a == -1) break;
23+
if (a == 0) {
24+
if (pQ.empty()) {
25+
cout << "-1" << endl;
26+
}
27+
else {
28+
cout << -pQ.top() << endl; //root 값
29+
pQ.pop();
30+
}
31+
}
32+
else {
33+
pQ.push(-a);
34+
}
35+
}
36+
}
37+
38+
//int main() {
39+
// ex074();
40+
// cout << endl << "done!" << endl;
41+
//}
42+
//3
43+
//5
44+
//2

lecture1/lecture1/ex075.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include<algorithm>
5+
6+
using namespace std;
7+
8+
// 최대 수입 스케쥴(priority_queue 응용문제)
9+
10+
struct Data {
11+
int money, when;
12+
Data(int a, int b) {
13+
money = a;
14+
when = b;
15+
}
16+
bool operator<(const Data &b) const {
17+
//return x < b.x; // 오른차순
18+
return when > b.when; // 내림차순
19+
}
20+
};
21+
22+
void ex075() {
23+
int n = 6;
24+
vector<vector<int>> inputs{ {50, 2},
25+
{20, 1},
26+
{40, 2},
27+
{60, 3},
28+
{30, 3},
29+
{30, 1}};
30+
vector<Data> T;
31+
priority_queue<int> pQ;
32+
int max= 0;
33+
for (int i = 0; i < inputs.size(); i++) {
34+
T.push_back(Data(inputs[i][0], inputs[i][1]));
35+
if (inputs[i][1] > max) {
36+
max = inputs[i][1];
37+
}
38+
}
39+
40+
sort(T.begin(), T.end());
41+
int res = 0;
42+
for (int i = max; i > 0; i--) {
43+
for (int j = 0; j < inputs.size(); j++){
44+
if (T[j].when < i) break;
45+
if (T[j].when > i) continue;
46+
pQ.push(T[j].money);
47+
}
48+
if (!pQ.empty()) {
49+
res += pQ.top();
50+
pQ.pop();
51+
}
52+
}
53+
54+
cout << res << endl;
55+
}
56+
57+
//int main() {
58+
// ex075();
59+
// cout << endl << "done!" << endl;
60+
//}
61+
// 150

lecture1/lecture1/ex077.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
// 친구인가? (Disjoint-set(서로수 집합) : Union&Find 자료구조)
8+
// 서로 다른 집합에서 공통 원소가 없으면 서로수 집합.
9+
10+
static int unf[1001];
11+
12+
static int Find(int v) {
13+
if (v == unf[v]) return v;
14+
else return unf[v] = Find(unf[v]);
15+
}
16+
17+
static void Union(int a, int b) {
18+
a = Find(a);
19+
b = Find(b);
20+
if (a != b) unf[a] = b;
21+
}
22+
23+
int ex077() {
24+
int n = 9; // 학생 수
25+
int m = 7; // 숫자 쌍의 개수
26+
vector<vector<int>> inputs{ {1, 2},
27+
{2, 3},
28+
{3, 4},
29+
{1, 5},
30+
{6, 7},
31+
{7, 8},
32+
{8, 9}};
33+
34+
for (int i = 1; i <= n; i++) {
35+
unf[i] = i;
36+
}
37+
38+
for (int i = 0; i < m; i++){
39+
Union(inputs[i][0], inputs[i][1]);
40+
}
41+
42+
if (Find(3) == Find (8)) cout << "YES" << endl;
43+
else cout << "NO" << endl;
44+
45+
return 0;
46+
}
47+
48+
//int main() {
49+
// ex077();
50+
// cout << endl << "done!" << endl;
51+
//}
52+
////NO

lecture1/lecture1/ex078.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
// 원더랜드(Kruskal MST 알고리즘 : Union&Find 활용)
8+
9+
struct Edge {
10+
int v1, v2, val;
11+
Edge(int a, int b, int c) {
12+
v1 = a;
13+
v2 = b;
14+
val = c;
15+
}
16+
bool operator<(const Edge &b) const {
17+
return val < b.val; // 오른차순
18+
}
19+
};
20+
21+
static int unf[1001];
22+
23+
static int Find(int v) {
24+
if (v == unf[v]) return v;
25+
else return unf[v] = Find(unf[v]);
26+
}
27+
28+
static void Union(int a, int b) {
29+
a = Find(a);
30+
b = Find(b);
31+
if (a != b) unf[a] = b;
32+
}
33+
34+
int ex078() {
35+
int n = 9; // 도시
36+
int m = 12; // 간선
37+
vector<vector<int>> inputs{ {1, 2, 12},
38+
{1, 9, 25},
39+
{2, 3, 10},
40+
{2, 8, 17},
41+
{2, 9, 8 },
42+
{3, 4, 18},
43+
{3, 7, 55},
44+
{4, 5, 44},
45+
{5, 6, 60},
46+
{5, 7, 38},
47+
{7, 8, 35},
48+
{8, 9, 15}};
49+
50+
for (int i = 1; i <= n; i++) {
51+
unf[i] = i;
52+
}
53+
vector<Edge> Ed;
54+
for (int i = 0; i < m; i++) {
55+
Ed.push_back(Edge(inputs[i][0], inputs[i][1], inputs[i][2]));
56+
}
57+
sort(Ed.begin(), Ed.end());
58+
int res = 0;
59+
for (int i = 0; i < m; i++) {
60+
int fa = Find(Ed[i].v1);
61+
int fb = Find(Ed[i].v2);
62+
if (fa != fb) {
63+
res += Ed[i].val;
64+
Union(Ed[i].v1, Ed[i].v2);
65+
}
66+
}
67+
cout << res << endl;
68+
69+
return 0;
70+
}
71+
72+
//int main() {
73+
// ex078();
74+
// cout << endl << "done!" << endl;
75+
//}
76+
//// 196

lecture1/lecture1/lecture1.vcxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,14 @@
194194
<ClCompile Include="ex068.cpp" />
195195
<ClCompile Include="ex070.cpp" />
196196
<ClCompile Include="ex071.cpp" />
197+
<ClCompile Include="ex072.cpp" />
198+
<ClCompile Include="ex073.cpp" />
199+
<ClCompile Include="ex074.cpp" />
200+
<ClCompile Include="ex075.cpp" />
201+
<ClCompile Include="ex077.cpp" />
202+
<ClCompile Include="ex078.cpp" />
197203
<ClCompile Include="tt.cpp" />
204+
<ClCompile Include="vector_with_struct.cpp" />
198205
</ItemGroup>
199206
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
200207
<ImportGroup Label="ExtensionTargets">

lecture1/lecture1/lecture1.vcxproj.filters

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,26 @@
231231
<ClCompile Include="ex071.cpp">
232232
<Filter>소스 파일</Filter>
233233
</ClCompile>
234+
<ClCompile Include="ex072.cpp">
235+
<Filter>소스 파일</Filter>
236+
</ClCompile>
237+
<ClCompile Include="ex073.cpp">
238+
<Filter>소스 파일</Filter>
239+
</ClCompile>
240+
<ClCompile Include="ex074.cpp">
241+
<Filter>소스 파일</Filter>
242+
</ClCompile>
243+
<ClCompile Include="ex075.cpp">
244+
<Filter>소스 파일</Filter>
245+
</ClCompile>
246+
<ClCompile Include="vector_with_struct.cpp">
247+
<Filter>소스 파일</Filter>
248+
</ClCompile>
249+
<ClCompile Include="ex078.cpp">
250+
<Filter>소스 파일</Filter>
251+
</ClCompile>
252+
<ClCompile Include="ex077.cpp">
253+
<Filter>소스 파일</Filter>
254+
</ClCompile>
234255
</ItemGroup>
235256
</Project>

0 commit comments

Comments
 (0)