-
Notifications
You must be signed in to change notification settings - Fork 0
[3월 18일] 브루트포스 #5
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uBE0C\uB8E8\uD2B8\uD3EC\uC2A4"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <stack> | ||
|
|
||
| using namespace std; | ||
|
|
||
| void Add(string A, string B) { | ||
| stack<int> a, b, res; | ||
|
|
||
| for (int i = 0; i < A.length(); i++) { | ||
| //문자를 정수로 바꾸기 | ||
| int x = A[i] - '0'; | ||
| // 계산하기 위해 스택에 넣기 (일의 자리부터) | ||
| a.push(x); | ||
| } | ||
|
|
||
| for (int i = 0; i < B.length(); i++) { | ||
| int y = B[i] - '0'; | ||
| // 계산하기 위해 스택에 넣기 (일의 자리부터) | ||
| b.push(y); | ||
| } | ||
|
|
||
| // 스택 이용하여 계산 | ||
| // 덧셈 시 올려 더해줘야되는 수 저장 | ||
| int up = 0; | ||
|
|
||
| // 두 수 중에 더 큰 자리수를 cnt에 저장 | ||
| int cnt = max(A.length(), B.length()); | ||
|
|
||
| while (cnt--) { | ||
| // 만약 한 스택이 다 비면 남은 스택만 혼자 계산해주면 됨 | ||
| if (a.empty() && !b.empty()) { | ||
| while (!b.empty()) { | ||
| res.push((b.top() + up) % 10); | ||
| up = (b.top() + up) / 10; | ||
| b.pop(); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| else if (b.empty() && !a.empty()) { | ||
| while (!a.empty()) { | ||
| res.push((a.top() + up) % 10); | ||
| up = (a.top() + up) / 10; | ||
| a.pop(); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| else if (a.empty() && b.empty()) break; | ||
|
|
||
| // 계산 | ||
| res.push((a.top() + b.top() + up) % 10); | ||
| up = (a.top() + b.top() + up) / 10; | ||
| a.pop(); | ||
| b.pop(); | ||
| } | ||
|
|
||
| // 결과 출력 | ||
| if (up != 0) cout << up; | ||
| while (!res.empty()) { | ||
| cout << res.top(); | ||
| res.pop(); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| string A, B; | ||
| cin >> A >> B; | ||
| Add(A, B); | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // 틀린 코드입니다..! 피드백 해주시면 참고하여 수정하겠습니다! | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
|
|
||
| using namespace std; | ||
|
|
||
| /* 제 시도는 일단 | ||
| 1. 먼저 '개수' 충족하는 문자열을 모두 만들고 (조합 이용하여) | ||
| * 2. 그 문자열에 모음이 하나라도 없으면 안된다는 조건을 확인하여 걸러내는 작업을 하고 싶었는데요.. | ||
| * next_permutation을 사용해보려고 했는데 아직 익숙치가 않아서.. 잘 모르겠습니다.. | ||
| */ | ||
|
|
||
|
|
||
| int main() { | ||
| int l, c; | ||
| // l = 암호 길이, c =문자 종류 개수 | ||
| cin >> l >> c; | ||
| // 문자 종류 담는 벡터 | ||
| vector<char> ch(c); | ||
|
|
||
| // permutation 사용 위한 벡터 | ||
| vector<int> temp(c); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. |
||
| // 결과 출력 위한 벡터 | ||
| vector<char> res; | ||
|
|
||
| // 입력 | ||
| for (int i = 0; i < c; i++) cin >> ch[i]; | ||
|
|
||
| // permutation 위해서, temp 벡터에 l개 만큼 1을 넣어준다 | ||
| for (int i = 0; i < l; i++) temp.push_back(1); | ||
| for (int i = l; i < c; i++) temp.push_back(0); | ||
|
|
||
| // 정렬해야 | ||
| sort(ch.begin(), ch.end()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. 그러나 사실 위 결과대로 출력해도 제대로된 결과를 얻지 못합니다. 왜냐하면 모든 경우의 수를 구할 수는 있지만, 사전순으로 단어들을 출력하지는 못하기 때문입니다. temp를 오름차순 정렬한 후 next_permutation으로 조합을 구하면 temp는 반복문을 돌때마다 아래와 같은 변화를 거치게 됩니다. 따라서 위 코드의 결과는 아래와같이 사전순과 정 반대되는 순서로 문자열을 출력하게 됩니다! 이럴때에는 내림차순 정렬된 컨테이너에서도 조합을 구할 수 있도록 해주는 prev_permutaion을 이용해볼 수 있습니다! 관련 링크 첨부하니 참고하셔서 문제 해결에 도움이 되었으면 하는 바람입니다 :) https://twpower.github.io/82-next_permutation-and-prev_permutation |
||
|
|
||
| do { | ||
| for (int i = 0; i < c; i++) { | ||
| // 모음 개수 | ||
| int v_cnt = 0; | ||
| if (temp[i] == 1) { | ||
| res.push_back(ch[i]); | ||
| if (ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o' || ch[i] == 'u') v_cnt++; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. |
||
|
|
||
| if (i == c - 1 && v_cnt < 1) continue; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. |
||
| else if (i == c - 1 && v_cnt >= 1) { | ||
| for (int j = 0; j < l; j++) cout << res[j]; | ||
| } | ||
| else continue; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. |
||
| } | ||
| } | ||
| cout << '\n'; | ||
| } while (next_permutation(temp.begin(), temp.end())); | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <string> | ||
| using namespace std; | ||
|
|
||
| int main() { | ||
| int n; | ||
| // 테스트케이스 개수 | ||
| cin >> n; | ||
|
|
||
| // n개씩 할당된 배열들 = 각각 테스트 되는 정수, 해당 정수의 스트라이크 개수와 볼 개수를 저장함 | ||
| vector<string> test(n); | ||
| vector<int> strk(n), ball(n); | ||
|
|
||
| // 최종적으로 조건 만족하는 정수들을 넣는 벡터 | ||
| // 구하고자 하는 개수는 벡터의 크기와 같음 | ||
| vector<int> ans; | ||
|
|
||
| // 입력 | ||
| for (int i = 0; i < n; i++) { | ||
| cin >> test[i] >> strk[i] >> ball[i]; | ||
| } | ||
|
|
||
|
|
||
| // 가능한 정수들에 대해 일일이 비교 | ||
| for (int i = 123; i <= 987; i++) { | ||
| // 각 자리수를 비교할 거라 문자열 형태로 바꾸어줌 | ||
| string x = to_string(i); | ||
|
|
||
| // 여기서 시간 오래 걸림.. 문제 조건 제발 잘 확인!!! | ||
| // 0 사용 불가 & 각 자리수 다 다른 숫자임!! | ||
| if (x[0] == '0' || x[1] == '0' || x[2] == '0' || x[0] == x[1] || x[1] == x[2] || x[0] == x[2]) continue; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 숫자에 0이 포함되어있을 경우 답으로 카운트될 수 없다는 코너케이스를 잘 캐치해주셨네요👍👍👍 |
||
|
|
||
| // 숫자 i에 대해 숫자 야구 진행해서 -> 모든 테스트 케이스와 각각의 결과 같은지 비교 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞아요! 모든 경우의 수를 다 해보는 것 이게 바로 브루트포스죠 👍 |
||
| for (int j = 0; j < n; j++) { | ||
| int test_strk = 0, test_ball = 0; | ||
|
|
||
| // 각 자리수에 대해, 즉 총 3회 비교 | ||
| for (int k = 0; k < 3; k++) { | ||
| // 스트라이크 확인 | ||
| if (x[k] == test[j][k]) test_strk++; | ||
|
|
||
| // 볼 확인 | ||
| if (x[k] == test[j][(k + 1) % 3] || x[k] == test[j][(k + 2) % 3]) test_ball++; | ||
| } | ||
|
|
||
| // 모든 테스트 케이스에 대해 결과값 같을 때 해당하는 정수 i를 ans 벡터에 넣어줌 | ||
| if (test_strk == strk[j] && test_ball == ball[j]) { | ||
| if (j == n - 1) ans.push_back(i); | ||
| continue; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. |
||
| } | ||
| else break; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. |
||
| } | ||
| } | ||
|
|
||
| // 모든 조건 만족하는 정수가 들어있는 벡터의 사이즈를 출력 | ||
| cout << ans.size(); | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| /* 질문. 이 문제에서 '메모리 사용량'을 줄일 수 있는 방법이 있을까요..? 무엇이 메모리 사용량을 크게 만드는 요소인지 궁금합니다! | ||
| * C++를 사용하여 문제를 푸신 다른 분들의 제출에 비해 제 코드가 메모리 사용량이 크더라구요.. | ||
| */ | ||
|
|
||
|
|
||
| // diff가 작은 순서대로 정렬하는 비교함수 | ||
| bool cmp(pair<int, int> v1, pair<int, int> v2) { | ||
| return v1.first < v2.first; | ||
| } | ||
|
|
||
|
|
||
| void Blackjack(int n, int m) { | ||
| // 카드값 입력 받기 위한 벡터 | ||
| vector<int> v(n); | ||
|
|
||
| // 차이값, 합 저장하기 위한 벡터 -> 이후에 차이값을 작은 순서대로 정렬해서 -> 가장 작은 차이값을 가질 때의 합을 출력하도록 할 것임 | ||
| vector<pair<int, int>> res; | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| // 카드 입력받아서 벡터에 저장 | ||
| cin >> v[i]; | ||
| } | ||
|
|
||
| int sum = 0; | ||
| // 3장이니까 그냥 차례차례 3개씩 묶어주는데 이때 시작 조건 주의했어야!! | ||
| // j,k는 각각 i,j의 다음(즉, i+1, j+1)부터라는 게 중요 | ||
| for (int i = 0; i < n - 2; i++) { | ||
| for (int j = i + 1; j < n - 1; j++) { | ||
| for (int k = j + 1; k < n; k++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p2. |
||
| // 각 카드의 합을 더해줌 | ||
| sum = v[i] + v[j] + v[k]; | ||
| int diff = m - sum; | ||
| // diff < 0 이라는 건 합이 m을 넘는다는 거니까 조건 충족 X | ||
| if (diff < 0) continue; | ||
| // 조건 충족시에만 차이값, 합을 벡터에 넣어준다 | ||
| else res.push_back({ diff, sum }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1. 그런데 과연 조건을 하나만 더 추가하면 굳이 번거롭게 벡터를 만들고 모든 경우의 수를 푸시시켜 정렬시키지 않고도 간단하게 정답을 구할 수 있을 것 같아요! res에 M을 넘지 않으면서 M에 최대한 가까운 카드 3장의 합을 담는다고 해볼께요 sum은 반드시 m보다 작아야하죠? 그렇다면 res를 어느 경우에 갱신시킬 수 있을까요? |
||
| } | ||
| } | ||
| } | ||
|
|
||
| // 차이가 작은 순서대로 정렬 -> v[0]에는 차이값이 가장 작을 때의 경우! | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위 커멘트를 참고하셔서 고치신다면 이부분도 필요가 없어지겠죠?! |
||
| sort(res.begin(), res.end(), cmp); | ||
|
|
||
| // 그때의 합을 출력해주면 됨 | ||
| cout << res[0].second; | ||
| } | ||
|
|
||
| int main() { | ||
| int n, m; | ||
| cin >> n >> m; | ||
| Blackjack(n, m); | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <cmath> | ||
|
|
||
| using namespace std; | ||
|
|
||
| // 다소 무식하게 연립방정식 & 근의 공식을 사용했습니다.. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전혀 그렇지 않은걸요🥺🥺 |
||
| void Tile(const int& R, const int& B) { | ||
|
|
||
| int res1 = (R + 4) / 2; | ||
| int res2 = R + B; | ||
|
|
||
| int x = (res1 + sqrt(res1 * res1 - 4 * res2)) / 2; | ||
| int y = res1 - x; | ||
|
|
||
| cout << max(x, y) << ' ' << min(x, y); | ||
| } | ||
|
|
||
| int main() { | ||
| int R, B; | ||
| cin >> R >> B; | ||
| Tile(R, B); | ||
| return 0; | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p2. 먼저 두개의 스택이 비어있지 않을 동안 값들을 계산하는 처리를 먼저 해주고, 그 이후에 A,B 스택에 남아있는 값들을 처리해주어도 괜찮았을 것 같아요! 아래 코드처럼요!