-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "01_\uC815\uB82C_\uB9F5_\uC14B"
Changes from 1 commit
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,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'; | ||
} | ||
if('0'<=s2[i] && s2[i]<='9'){ | ||
s2_sum+=s2[i]-'0'; | ||
} | ||
} | ||
Comment on lines
+11
to
+19
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. 아주 깔끔한 로직으로 비교함수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; | ||
} |
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
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와 이진탐색을 이용하여 문제를 풀어 주셨네요!👍그런데, 직접 값을 정렬해 주지 않아도 값을 삽입하면 정렬된 상태로 저장해 주는 컨테이너가 있었죠! 그걸 사용해 보면 어떨까요?😉 |
||
|
||
cout << num; | ||
|
||
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.
그리고, string헤더의 isdigit 함수를 이용하여 문자의 숫자 여부를 판별할 수 있다는 점도 고려해 주시면 좋을 것 같습니다.😊