1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
 
int number = 9;
int heap[9= { 7,6,5,8,3,5,9,1,6 };
 
int main() {
    //힙을 구성
    for (int i = 1; i < number; i++) {
        int c = i;
        do {
            int root = (c - 1/ 2;
            if (heap[root] < heap[c]) {
                int temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            c = root;
        } while (c != 0);
    }
    //크기를 줄여가며 반복적으로 힙을 구성
    for (int i = number - 1; i >= 0; i--) {
        int temp = heap[0];
        heap[0= heap[i];
        heap[i] = temp;
        int root = 0;
        int c = 1;
        do {
            c = 2 * root + 1;
            //자식 중에 더 큰 값을 찾기
            if (c < i - 1 && heap[c] < heap[c + 1]) {
                c++;
            }
            //루트보다 자식이 크다면 교환
            if (c < i && heap[root] < heap[c]) {
                temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            root = c;
        } while (c < i);
    }
    //결과 출력
    for (int i = 0; i < number; i++) {
        printf("%d ", heap[i]);
    }
}
cs

 

'C,C++ > Algorithm' 카테고리의 다른 글

Algorithm, c++, Sort  (0) 2019.09.23
Algorithm, c++, BFS와 DFS 구현  (0) 2019.09.23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
 
using namespace std;
 
bool compare(pair<intpair<stringint>> v1, pair<intpair<stringint>> v2) {
    if (v1.second.second != v2.second.second) {
        return v1.second.second > v2.second.second;
    }
    else {
        return v1.first < v2.first;
    }
}
 
int main() {
    vector<pair<intpair<stringint>>> v;
 
 
    for (int i = 0; i < 5; i++) {
        string name;
        int score;
        cin >> name >> score;
        v.push_back(pair<intpair<stringint>>(i, make_pair(name, score)));
    }
 
    sort(v.begin(), v.end(), compare);
 
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].second.first << endl;
    }
}
cs

 

'C,C++ > Algorithm' 카테고리의 다른 글

Algorithm, c++, Heap sort  (0) 2019.09.23
Algorithm, c++, BFS와 DFS 구현  (0) 2019.09.23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <queue>
#include <stack>
 
using namespace std;
 
int number = 7;
bool c[7];
vector<int> a[8];
 
void bfs(int start) {
    queue<int> q;
    q.push(start);
    c[start] = true;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        cout << x << ' ';
        for (int i = 0; i < a[x].size(); i++) {
            int y = a[x][i];
            if (!c[y]) {
                q.push(y);
                c[y] = true;
            }
        }
    }
}
 
void dfs(int start) {
    if (c[start]) return;
    c[start] = true;
    cout << start << ' ';
    for (int i = 0; i < a[start].size(); i++) {
        int y = a[start][i];
        dfs(y);
    }
}
 
void linkEdge(int x, int y) {
    a[x].push_back(y);
    a[y].push_back(x);
}
 
int main() {
    linkEdge(12);
    linkEdge(13);
    linkEdge(23);
    linkEdge(24);
    linkEdge(25);
    linkEdge(45);
    linkEdge(36);
    linkEdge(37);
    linkEdge(67);
 
 
    dfs(1);
 
    return 0;
}
cs

bfs 출력 결과 :

1 2 3 4 5 6 7 

 

dfs 출력 결과 :

1 2 3 6 7 4 5

 

'C,C++ > Algorithm' 카테고리의 다른 글

Algorithm, c++, Heap sort  (0) 2019.09.23
Algorithm, c++, Sort  (0) 2019.09.23

+ Recent posts