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

[ 정렬, 맵, 셋 ] 8월 23일 #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions 01_정렬_맵_셋/1431.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool cmp(const string &s1, const string &s2){
if(s1.size()!=s2.size()){
return s1.size()<s2.size();
}
int s1_sum=0, s2_sum=0;
for(int i=0;i<s1.size();i++){
if('0'<=s1[i] && s1[i]<='9'){
s1_sum+=s1[i]-'0';
}
Comment on lines +13 to +15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고, string헤더의 isdigit 함수를 이용하여 문자의 숫자 여부를 판별할 수 있다는 점도 고려해 주시면 좋을 것 같습니다.😊

if('0'<=s2[i] && s2[i]<='9'){
s2_sum+=s2[i]-'0';
}
}
Comment on lines +11 to +19
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아주 깔끔한 로직으로 비교함수cmp를 잘 구현해 주셨네요!😊 핵심인 자릿수 합 구하기도 잘 작성해 주셨습니다. cmp에는 문제의 세 가지 조건을 비교하는 부분만 남기고, 이 부분은 따로 함수로 빼내어 cmp에서 호출하여 사용한다면 더 깔끔한 코드가 될 수 있을 것 같습니다!👍👍

if(s1_sum!=s2_sum){
return s1_sum<s2_sum;
}
return s1<s2;
}

int main(){

int n;
cin >> n;

vector<string> arr(n);
for(int i=0;i<n;i++){
cin >> arr[i];
}

sort(arr.begin(), arr.end(), cmp);

for(int i=0;i<arr.size();i++){
cout << arr[i] << '\n';
}

return 0;
}
33 changes: 33 additions & 0 deletions 01_정렬_맵_셋/14425.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n, m, num;
string stm;

cin >> n >> m;

vector<string> arr(n);
for(int i=0;i<n;i++){
cin >> arr[i];
}
sort(arr.begin(), arr.end());

for(int i=0;i<m;i++){
cin >> stm;
if(binary_search(arr.begin(), arr.end(), stm)){
num++;
}
}
Comment on lines +21 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort와 이진탐색을 이용하여 문제를 풀어 주셨네요!👍그런데, 직접 값을 정렬해 주지 않아도 값을 삽입하면 정렬된 상태로 저장해 주는 컨테이너가 있었죠! 그걸 사용해 보면 어떨까요?😉


cout << num;

return 0;
}