Skip to content

Commit

Permalink
Update: C++ Code(Interview)
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 committed Jun 12, 2024
1 parent 7d3982e commit e525363
Show file tree
Hide file tree
Showing 22 changed files with 407 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"files.associations": {
"*.yarnproject": "jsonc",
"iostream": "cpp"
}
},
"C_Cpp.errorSquiggles": "disabled"
}
90 changes: 90 additions & 0 deletions 1260번: DFS와 BFS/DFS와 BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: ::: ::: */
/* Problem Number: 1260 :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fkdl4878 <boj.kr/u/fkdl4878> +#+ +#+ +#+ */
/* +#+ +#+ +#+ */
/* https://boj.kr/1260 #+# #+# #+# */
/* Solved: 2024/06/11 01:02:51 by fkdl4878 ### ### ##.kr */
/* */
/* ************************************************************************** */

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

void dfs(vector<vector<int>>& graph, vector<bool>& visited, int start) {
stack<int> s;
s.push(start);
visited[start] = true;
cout << start << " ";

while (!s.empty()) {
int current = s.top();
s.pop();

for (int i = 0; i < graph[current].size(); ++i) {
int next = graph[current][i];
if (!visited[next]) {
cout << next << " ";
visited[next] = true;
s.push(current);
s.push(next);
break;
}
}
}
}

void bfs(vector<vector<int>>& graph, vector<bool>& visited, int start) {
queue<int> q;
q.push(start);
visited[start] = true;

while (!q.empty()) {
int current = q.front();
q.pop();
cout << current << " ";

for (int i = 0; i < graph[current].size(); ++i) {
int next = graph[current][i];
if (!visited[next]) {
visited[next] = true;
q.push(next);
}
}
}
}

int main() {
int n, m, v;
cin >> n >> m >> v;

vector<vector<int>> graph(n + 1);
vector<bool> visited(n + 1, false);

for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}

for (int i = 1; i <= n; ++i) {
sort(graph[i].begin(), graph[i].end());
}

dfs(graph, visited, v);
cout << endl;

fill(visited.begin(), visited.end(), false);
bfs(graph, visited, v);
cout << endl;

return 0;
}
34 changes: 34 additions & 0 deletions 1260번: DFS와 BFS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 1260번: DFS와 BFS - <img src="https://static.solved.ac/tier_small/9.svg" style="height:20px" /> Silver II

<!-- performance -->

<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.-->

<!-- end -->

## 문제

[문제 링크](https://boj.kr/1260)


<p>그래프를 DFS로 탐색한 결과와 BFS로 탐색한 결과를 출력하는 프로그램을 작성하시오. 단, 방문할 수 있는 정점이 여러 개인 경우에는 정점 번호가 작은 것을 먼저 방문하고, 더 이상 방문할 수 있는 점이 없는 경우 종료한다.&nbsp;정점 번호는 1번부터 N번까지이다.</p>



## 입력


<p>첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사이에 여러 개의 간선이 있을 수 있다. 입력으로 주어지는 간선은 양방향이다.</p>



## 출력


<p>첫째 줄에 DFS를 수행한 결과를, 그 다음 줄에는 BFS를 수행한 결과를 출력한다. V부터 방문된 점을 순서대로 출력하면 된다.</p>



## 소스코드

[소스코드 보기](DFS와%20BFS.cpp)
9 changes: 9 additions & 0 deletions 2024/C++/ArraySize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <bits/stdc++.h>

using namespace std;

int main() {
int arr[5] = {1, 2, 3, 4, 5};
cout << "Array size: " << sizeof(arr) / sizeof(arr[0]) << endl;
return 0;
}
Binary file added 2024/C++/ArraySize.exe
Binary file not shown.
44 changes: 44 additions & 0 deletions 2024/C++/DFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>

// 노드 구조체 정의
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
TreeNode(int v) : value(v), left(nullptr), right(nullptr) {}
};

// 전위 순회 함수 정의
void preOrderTraversal(TreeNode* root) {
if (root == nullptr) return;
std::cout << root->value << " "; // 현재 노드 방문
preOrderTraversal(root->left); // 왼쪽 자식 방문
preOrderTraversal(root->right); // 오른쪽 자식 방문
}

// 노드 삽입 함수 정의
TreeNode* insert(TreeNode* root, int value) {
if (root == nullptr) return new TreeNode(value);
if (value < root->value) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}

int main() {
TreeNode* root = nullptr;
root = insert(root, 5);
root = insert(root, 122);
root = insert(root, 1);
root = insert(root, 4);
root = insert(root, 2);
root = insert(root, 3);

// 전위 순회를 통해 요소를 방문
preOrderTraversal(root);
std::cout << std::endl;

return 0;
}
Binary file added 2024/C++/DFS.exe
Binary file not shown.
27 changes: 27 additions & 0 deletions 2024/C++/DP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <vector>

using namespace std;

long long fibonacci(int n) {
if (n <= 1)
return n;

vector<long long> dp(n + 1);
dp[0] = 0;
dp[1] = 1;

for (int i = 2; i <= n; ++i){
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n];
}

int main() {
int n = 50; // 예를 들어 50번째 피보나치 수를 구하고자 함

std::cout << "Fibonacci(" << n << ") = " << fibonacci(n) << std::endl;

return 0;
}
Binary file added 2024/C++/DP.exe
Binary file not shown.
17 changes: 17 additions & 0 deletions 2024/C++/L_R_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <vector>

class MyClass {
public:
std::vector<int> data;

MyClass(std::vector<int> d) : data(std::move(d)) {
// R-value 참조를 사용하여 벡터의 데이터를 이동
}
};

int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
MyClass obj(std::move(vec)); // vec의 내용을 이동
std::cout << "vec size: " << vec.size() << std::endl; // vec는 비어 있음
}
Binary file added 2024/C++/L_R_value.exe
Binary file not shown.
15 changes: 15 additions & 0 deletions 2024/C++/PointerRef.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <bits/stdc++.h>

using namespace std;

int main()
{
int a = 10;
int *b = &a;

cout << b << endl;

b++;

cout << b << endl;
}
Binary file added 2024/C++/PointerRef.exe
Binary file not shown.
24 changes: 24 additions & 0 deletions 2024/C++/SmartPointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>

using namespace std;

class cat {
public:
cat() {
cout << "cat created" << endl;
}

~cat() {
cout << "cat destroyed" << endl;
}
};


int main() {
shared_ptr<cat> c1 = make_shared<cat>();
cout << "c1 use count: " << c1.use_count() << endl;
shared_ptr<cat> c2 = c1;
cout << "c1 use count: " << c1.use_count() << endl;

return 0;
}
Binary file added 2024/C++/SmartPointer.exe
Binary file not shown.
18 changes: 18 additions & 0 deletions 2024/Codility/Task01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <bits/stdc++.h>

using namespace std;

int solution(string &P, string &Q){
int n = P.size();

cout << n << "\n";
}

int main() {
string P = "CAGCCTA";
string Q = "TTTTTTT";

cout << solution(P, Q) << "\n";

return 0;
}
Binary file added 2024/Codility/Task01.exe
Binary file not shown.
5 changes: 4 additions & 1 deletion 2024/CodingTest/SummaryNote.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@

long long을 쓰고, 제곱연산을 생각

### 배수
### 배수

## 그래프

24 changes: 24 additions & 0 deletions 2024/LeetCode/704.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;

while(left <= right){
int pivot = (left + right) / 2;

if (nums[pivot] == target){
return pivot;
}

if (nums[pivot] < target){
left = pivot + 1;
}
else{
right = pivot - 1;
}
}

return -1;
}
};
Empty file added 2024/LeetCode/724.cpp
Empty file.
Loading

0 comments on commit e525363

Please sign in to comment.