Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

naw0n #50

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions WEEK3-B/naw0n/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## string find, replace 함수
### 1. find 함수 <br>
문자열에서 원하는 문자열의 위치를 찾는 함수 <br>

1. `pos(검색을 시작할 위치)`에서 부터 탐색 시작
2. 문자열에서 `str(찾고자 하는 문자열)` 위치 리턴
3. `s(찾고자 하는 문자열을 가리키는 포인터)`가 가리키는 문자부터 `count(찾고자 하는 문자열의 길이)`개만큼 취한 부분 문자열을 초기 문자열에서 찾는다.
4. `s`가 가리키는 문자열은 초기 문자열에서 검색한다. 이때 `s`는 null 종료 문자열로 간주
5. 초기 문자열에서 문자 `ch(찾고자 하는 문자)` 위치 탐색
6. `t(찾고자 하는 string_view로 변환 가능한 객체`를 string_view 객체인 `sv`로 변환하고 초기 문자열에서 찾는다.

```c++
size_type find(const basic_string& str, size_type pos = 0) const;
size_type find(const CharT* s, size_type pos, size_type count) const;
size_type find(const CharT* s, size_type pos = 0) const;
size_type find(CharT ch, size_type pos = 0) const;
template <class T>
size_type find(const T& t, size_type pos = 0) const;
```
### 2. replace 함수
문자열에서 일부를 다른 문자열로 치환하는 함수 <br>
```c++
#include <iostream>
#include <string>
using namespace std;

int main(void) {
string word = "sweet new, sweet new";
cout << word << '\n';
word.replace(6, 3, "change"); // word[6]부터 3개의 문자를 "change"로 대체
cout << word << '\n';
return 0;
}
```
42 changes: 42 additions & 0 deletions WEEK3-B/naw0n/boj11723.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <string>
using namespace std;

int M, result, Bit;
string input;

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> M;
while (M--) {
input.clear();
cin >> input;

if (input == "add") {
cin >> result;
Bit |= (1 << result);
}
else if (input == "remove") {
cin >> result;
Bit &= ~(1 << result);
}
else if (input == "check") {
cin >> result;
if (Bit & (1 << result)) cout << 1 << '\n';
else cout << 0 << '\n';
}
else if (input == "toggle") {
cin >> result;
Bit ^= (1<< result);
}
else if (input == "all") {
Bit = (1 << 21) - 1;
}
else if (input == "empty") {
Bit = 0;
}
}
return 0;
}
18 changes: 18 additions & 0 deletions WEEK3-B/naw0n/boj15953.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
using namespace std;

int main() {
int T, a, b;
cin >> T;

int firstreward[100] = { 500, 300, 300, 200, 200, 200, 50, 50, 50, 50, 30, 30, 30, 30, 30, 10, 10, 10, 10, 10, 10 };
int secondreward[64] = { 512, 256, 256, 128, 128, 128, 128, 64,64,64,64,64,64,64,64,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32 };

for (int i = 0; i < T; i++) {
cin >> a >> b;
int sum = 10000 * (firstreward[a - 1] + secondreward[b - 1]);

cout << sum << endl;
}
return 0;
}
35 changes: 35 additions & 0 deletions WEEK3-B/naw0n/boj16926.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
using namespace std;
int n, m, r, arr[301][301];

void rotation() {
int tmp[301][301];
for (int angle = 0; angle < min(n, m) / 2; angle++) {
int x1 = angle, y1 = angle, x2 = n - angle - 1, y2 = m - angle - 1;
for (int i = y2 - 1; i >= y1; i--) tmp[x1][i] = arr[x1][i + 1]; // ��
for (int i = x1 + 1; i <= x2; i++) tmp[i][y1] = arr[i - 1][y1]; //��
for (int i = y1 + 1; i <= y2; i++) tmp[x2][i] = arr[x2][i - 1]; //��
for (int i = x2 - 1; i >= x1; i--) tmp[i][y2] = arr[i + 1][y2]; // ��
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
arr[i][j] = tmp[i][j];
}
}

int main() {
cin >> n >> m >> r;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
while (r--) rotation();

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
51 changes: 51 additions & 0 deletions WEEK3-B/naw0n/boj1790.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <string>
using namespace std;

long long num_length(int n) {
long long idx = 0;
for (int start = 1, i = 1; start < n + 1; start *= 10, ++i) {
int end = start * 10 - 1;
if (end > n) {
end = n;
}
idx += (long long)(end - start + 1) * i;
}
return idx;
}

int binarySearch(int left, int right, int key) {
if (left >= right) {
return right;
}
int mid = (left + right) / 2;
long long idx = num_length(mid);
// 1���� mid���� ���� ���ο� �� ����<key �� ���
if (idx < key) {
return binarySearch(mid + 1, right, key); // [mid+1, right] ������ ��Ž��
}
// 1���� mid���� ���� ���ο� �� ����>=key �� ���
else
return binarySearch(left, mid, key); // [left,mid) ������ �� Ž��
}
int main() {
ios_base::sync_with_stdio(0);
int n, k;

cin >> n >> k;

// ���ο� ���� ���̰� k���� ���� ���
if (num_length(n) < k) {
cout << -1;
return 0;
}
int i_num = binarySearch(1, n, k);
int length = num_length(i_num);

string s_num = to_string(i_num);

cout << s_num[(s_num.size() - (length - k) - 1)];

return 0;

}
20 changes: 20 additions & 0 deletions WEEK3-B/naw0n/boj2941.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
vector<string> word = { "c=","c-","dz=","d-","lj","nj","s=","z=" };
int index;
string input;
cin >> input;
for (int i = 0; i < word.size(); i++) {
while (1) {
index = input.find(word[i]);
if (index == string::npos)
break;
input.replace(index,word[i].length(), "#");
}
}
cout << input.length();
}
25 changes: 25 additions & 0 deletions WEEK3-B/naw0n/boj5086.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
using namespace std;

int main() {
int a, b;
while (1) {
cin >> a >> b;

if (a == 0 && b == 0) {
break;
}

if (a % b == 0) {
cout << "multiple" << endl;
}
else if (b % a == 0) {
cout << "factor" << endl;
}
else {
cout << "neither" << endl;
}

}

}
64 changes: 64 additions & 0 deletions WEEK4-B/naw0n/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
## 재귀 함수
어떤 함수에서 자신을 다시 호출하여 작업을 수행하는 방식

```c++
void Binaryto(long long n) {
if (n != 1) Binaryto(n / 2);
cout << (n % 2);
}
```
#### 유의할 점
함수가 끝날 때 함수 호출 이후의 명령문이 수행되지 않음.<br>
종료 조건이 꼭 포함되어야 함<br>
#### 재귀함수의 단점
- 함수 호출 시 `함수의 매개 변수, 지역변수, 리턴값, 함수 종료 후 돌아가는 위치`가 스택 메모리에 저장
- 따라서, 메모리를 많이 차지 -> 성능이 반복문에 비해 느림.
- 함수를 반복적으로 호출하기 때문에 스택 메모리가 커지고, 호출하는 횟수가 많아지며 스택오버플로우가 발생할 수 있음.

## 정수를 문자열로, 문자열을 정수로
1. 정수->문자열(int to string) : to_string() 사용
2. 문자열->정수 : int로 변환 후 - 48

```c++
while (int(x.size()) != 1) {
int sum = 0;
for (unsigned i = 0; i < x.size(); i++) {
sum += int(x[i]) - 48;
}
x = to_string(sum);
i++;
}
```

## 분할정복
문제를 나눌 수 없을 때까지 나누어서 각각을 풀고, 다시 합쳐 문제의 답을 얻는 알고리즘

### (1) 분할정복 알고리즘 설계
1. Divide(분할) : 문제가 분할 가능하다면, 2개 이상의 문제로 나눈다.
2. ConQuer(정복) : 나눈 문제가 또 분할 가능하다면, `divide`를 또 다시 수행한다. 그렇지 않다면 문제를 푼다.
3. Combine(결합) : `Conquer`한 문제를 합해 본래 문제의 답을 찾는다.
* 단점 : 함수를 재귀적으로 호출해야 하기 때문에 함수 호출로 인한 오버헤드가 발생하며, 스택에 다양한 데이터를 보관하고 있어야 하므로 스택오버플로우가 발생하거나 과도한 메모리를 사용한다.

### (2) 분할정복 응용
#### 병합정렬(Merge Sort) - 시간복잡도 O(nlogn), 공간복잡도 O(n)
1. 정렬할 데이터 집합 크기가 0또는1 이면 이미 정렬된 것으로 본다.
2. 1의 경우에 해당하지 않으면, 데이터 집합을 반으로 나눈다.
3. 원래 같은 집합에서 나뉘어져 나온 데이터 집합 둘을 병합하여 하나의 데이터 집합으로 만든다. 단, 병합할 때의 데이터 집합의 원소는 순서에 맞춰 정렬한다.
4. 데이터 집합이 하나가 될 때 까지 3의 과정을 반복한다.

#### 이진탐색 T(n) = logn
1. Divide(분할) : 배열의 가운데 원소를 기준으로 왼쪽, 오른쪽 부분배열로 분할. 탐색키와 가운데 원소가 같으면 반환 및 종료
2. ConQuer(정복) : 탐색 키가 가운데 원소보다 작으면 왼쪽 부분 배열을 대상으로 이진탐색을 순환 호출, 크면 오른쪽 부분배열을 대상으로 이진탐색을 순환 호출
3. Combine(결합) : 탐색 결과가 직접 반환 되기 때문에 결합 불필요

#### 퀵 정렬
1. Divide(분할) : 피벗을 기준으로 배열을 두 부분 배열로 분할
2. ConQuer(정복) : 두 부분배열에 대해 퀵 정렬을 순환적으로 적용하여 각 부분배열을 정렬
3. Combine(결합) : 결합 불필요

#### 거듭제곱
원래 거듭제곱은 자신을 n번 곱해야 하므로 O(n)의 시간이 소요된다.<br><br>

지수를 반으로 나눈 거듭 제곱 알고리즘의 수행시간 : O(log<sub>2</sub>n) <br>
`지수가 짝수일 경우` ) 지수를 반으로 나눠서 곱한다. C<sup>(n/2)</sup> * C<sup>(n/2)</sup> <br>
`지수가 홀수일 경우` ) 지수에서 1을 빼고 반으로 나눠 곱하고, 밑을 한 번 더 곱한다. C<sup>(n-1)</sup>*C<sup>((n-1)/2)</sup>
37 changes: 37 additions & 0 deletions WEEK4-B/naw0n/boj1074.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
using namespace std;

int n, r, c;
int th;

void Z(int y, int x, int size)
{
if (y == r && x == c)
{
cout << th << endl;
return;
}

// r,c�� ���� ��и鿡 ������ ���
if (r < y + size && r >= y && c < x + size && c >= x)
{
// 1��и� Ž��
Z(y, x, size / 2);
// 2��и� Ž��
Z(y, x + size / 2, size / 2);
// 3��и� Ž��
Z(y + size / 2, x, size / 2);
// 4��и� Ž��
Z(y + size / 2, x + size / 2, size / 2);
}
else
{
th += size * size;
}
}
int main()
{
cin >> n >> r >> c;
Z(0, 0, (1 << n));
return 0;
}
16 changes: 16 additions & 0 deletions WEEK4-B/naw0n/boj10829.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
using namespace std;

void Binaryto(long long n) {
if (n != 1) Binaryto(n / 2);
cout << (n % 2);
}
int main() {
long long N;
cin >> N;

Binaryto(N);
cout << endl;

return 0;
}
24 changes: 24 additions & 0 deletions WEEK4-B/naw0n/boj13706.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
#include<cmath>

using namespace std;

double squareRoot(double N, long double key);
long long N;

int main()
{
cin >> N;
long long mid = N / 2;
cout << squareRoot(N, mid) << endl;
return 0;
}

double squareRoot(double N, long double key)
{
long double root = (0.5 * (key + (N / key)));
if (abs(key - root) >= 0.00000000000001)
return squareRoot(N, root);
else
return root;
}
Loading