Skip to content
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
29 changes: 29 additions & 0 deletions 9월 7일 - 정렬/1026.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
int n;
cin >> n;
int a[n], b[n];
bool check[n];
Copy link

Choose a reason for hiding this comment

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

배열도 좋지만, c++에 익숙해지기 위해 vector 사용도 추천드립니다!

for(int i=0; i<n; i++){
cin >> a[i];
}
for(int i=0; i<n; i++){
cin >> b[i];
}

sort(a, a+n);

sort(b, b+n);

int sum = 0;
for(int i=0; i<n; i++){
sum += a[i]*b[n-i-1];
Copy link

Choose a reason for hiding this comment

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

오오 둘 다 오름차순으로 해서 인덱스를 거꾸로 접근하신 거 좋네요 ! 👍 greater<>() 을 활용하여 내림차순 정렬을 해봐도 좋을 것 같아요!

}

cout << sum;
}

49 changes: 49 additions & 0 deletions 9월 7일 - 정렬/10994.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#include <iostream>

using namespace std;

void solve(int x, int y, int start_len, int length, char star[][400]){

if(x == start_len / 2 && y == start_len / 2){
star[x][y] = '*';
return;
}

for(int i=x; i<x+length; i++){
if(i==x || i==x+length-1){
for(int j=y; j<y+length; j++){
star[i][j] = '*';
Copy link

Choose a reason for hiding this comment

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

지금도 너무 잘해주셨는데, 별을 채우는 부분을 반복문 하나로도 작성할 수 있는 방법이 있어요! 별은 지금 테두리만 채워지고 있죠? 그리고 상, 하, 좌, 우 각각의 테두리는 행 또는 열이 고정되어 있어요. 이 점을 활용해보면 좋을 것 같아요 !

}
}
else{
star[i][y] = '*';
star[i][y+length-1] = '*';
}
}

solve(x+2, y+2, start_len, length-4, star);
Copy link

Choose a reason for hiding this comment

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

테두리 별이 4씩 줄어든다는 규칙을 아주 잘 파악하셨군요 ! 👍 재귀로 너무 잘 구현해주셨어요

}

int main(){
int n;
cin >> n;
int length = 1+(n-1)*4;
Copy link

Choose a reason for hiding this comment

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

괄호를 풀어 계산하면 식을 더 단순화 시킬 수 있을 것 같아요 👍


char star[400][400];

solve(0, 0, length, length, star);

for(int i=0; i<length; i++){
for(int j=0; j<length; j++){
if(star[i][j] == '*'){
cout << star[i][j];
}
else
cout << " ";
Copy link

Choose a reason for hiding this comment

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

2차원 벡터를 사용하여 처음 선언 시 " "로 초기화하면 star[i][j] 로 한 번에 출력이 가능해요! 2차원 벡터로 풀이해 보시는 걸 추천드려요 👍

}
cout << '\n';
}
}


23 changes: 23 additions & 0 deletions 9월 7일 - 정렬/11399.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
int n;
cin >> n;
int arr[n];

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

sort(arr, arr+n);
int sum = 0;
for(int i=0; i<n; i++){
sum += (n-i)*arr[i];
Copy link

Choose a reason for hiding this comment

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

O(n^2) 풀이를 생각하기가 쉬운데, O(n) 풀이를 해주시다니 최고에요! 👍👍👍

}

cout << sum;
}

32 changes: 32 additions & 0 deletions 9월 7일 - 정렬/11651.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct xy{
int x,y;
};
Copy link

Choose a reason for hiding this comment

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

구조체 사용도 좋지만, 인자가 2개 뿐이니 pair 자료형을 사용해보시는 걸 추천드려요~!


bool comp(const xy &i1, const xy &i2){
if(i1.y != i2.y)
return i1.y < i2.y;
return i1.x < i2.x;
Copy link

Choose a reason for hiding this comment

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

👍👍👍

}

int main(){
int n;
vector<xy> xy;
cin >> n;

xy.assign(n, {});
for(int i=0; i<n; i++){
cin >> xy[i].x >> xy[i].y;
}

sort(xy.begin(), xy.end(), comp);

for(int i=0; i<n; i++){
cout << xy[i].x << " " << xy[i].y << '\n';
}
}
34 changes: 34 additions & 0 deletions 9월 7일 - 정렬/1316.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
bool check = true;
int count = 0;

for(int i=0; i<n; i++){
check = true;
string s;
cin >> s;
int len = s.length();

for(int j=0; j<len; j++){
char ch = s.at(j);
Copy link

Choose a reason for hiding this comment

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

.at 사용도 좋지만, 바로 s[j] 로 접근하셔도 좋을 것 같아요~!


while(j<len-1 && s.at(j+1) == ch){
j++;
}

for(int k=j+1; k<len; k++){
if(s.at(k) == ch){
check = false;
Copy link

@bsa0322 bsa0322 Sep 7, 2021

Choose a reason for hiding this comment

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

p2. 여기서 바로 break로 빠져나오면 시간이 더 절약될 것 같아요! 하지만 이 중 반복문을 한번에 빠져나오기가 조금 번거롭죠. 이럴 때 함수화를 하시면 바로 return 해주면 돼서 편리해요! 또한 지금도 풀이 너무 잘해주셨는데, 이 문제는 O(n)으로 풀 수 있는 풀이법이 있어요. 지금은 현재 알파벳이 다음번에 등장하는지 미리 검사를 하고 있죠. 그러면 다음번에 계속 등장하지 않는 경우엔 불필요한 검사가 반복돼요. 현재 알파벳이 전에 등장했었는지를 검사하는 방향으로 가면 어떨까요? 이때, 등장 여부를 한 번에 체크할만한 무언가가 필요할 것 같아요.

}
}
}
if(check)
Copy link

Choose a reason for hiding this comment

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

함수화 하시면 check 변수의 사용도 필요 없어지고, 코드도 더 보기 편해져요 ! 그룹 단어 체크하는 부분 모듈화 추천드립니다!

count++;
}

cout << count;
}
33 changes: 33 additions & 0 deletions 9월 7일 - 정렬/13458.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#include <iostream>

using namespace std;

int main(){
int n;
cin >> n;
int arr[n];

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

int b, c;
cin >> b >> c;

long long count = 0;
for(int i=0; i<n; i++){

arr[i] -= b;
count++;

if(arr[i] > 0){
count += arr[i]/c;
if(arr[i]%c > 0){
count++;
Copy link

Choose a reason for hiding this comment

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

완벽해요! 👍

}
}
}

cout << count;
}
43 changes: 43 additions & 0 deletions 9월 7일 - 정렬/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;

struct serial{
string num;
int length, sum;
};

bool cmp(const serial &i1, const serial &i2){
if(i1.num.length() != i2.num.length())
Copy link

Choose a reason for hiding this comment

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

p2. 구조체에 length 변수를 만들어주셨는데 num에 접근하신 후 length() 함수를 사용하셨네요! 둘 중 하나만 하셔야 될 것 같아요. 이왕이면 구조체 사용없이 string num만 사용하시는 걸 추천드립니다! 그러면 인덱스에 계속 접근하는 부분도 깔끔해질 것 같아요. 지금 sum값을 미리 저장하시느라 구조체 사용을 하신 것 같은데, sum을 구하는 함수를 따로 만드시면 이 부분도 해결 될 것 같아요!

return i1.length < i2.length;
if(i1.sum != i2.sum)
return i1.sum < i2.sum;
return i1.num < i2.num;
}

int main(){
int n;
vector<serial> srl;
cin >> n;
srl.assign(n, {});

for(int i=0; i<n; i++){
cin >> srl[i].num;
srl[i].length = srl[i].num.length();
}

for (int i = 0; i < n; i++) {
for (int j = 0; j < srl[i].length; j++) {
if (srl[i].num.at(j) >= 48 && srl[i].num.at(j) <= 57)
Copy link

Choose a reason for hiding this comment

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

isdigit 함수에 대해 알아보면 좋을 것 같아요! 또한 아스키코드를 바로 사용하는 건 실수의 가능성이 있으니 '0' 과 '9' 로 사용하시는 걸 추천드려요!

srl[i].sum += srl[i].num.at(j)-'0';
}
}

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

for(int i=0; i<n; i++) {
cout << srl[i].num << '\n';
}
}
47 changes: 47 additions & 0 deletions 9월 7일 - 정렬/1946.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int solution(int n) {
vector<pair<int, int>> v;
v.assign(n, {});
Copy link

Choose a reason for hiding this comment

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

선언 시 한번에 초기화하는 걸 추천드려요!


for(int i=0; i<n; i++){
cin >> v[i].first >> v[i].second;
}
Copy link

Choose a reason for hiding this comment

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

p2. 가급적 입력은 main에서 처리해주시는 게 좋아요. vector를 함수의 인자로 넘기는 방법을 사용해주세요! 아래 정렬 부분도 마찬가지로 main 에서 해주시는 게 좋아요!

지금 함수에 너무 많은 기능이 담겨있어요. 모듈화를 하는 이유는 그 기능만 담아서 편리하게 사용하기 위해서인데, 작성해주신 solution 함수는 입력 + 정렬 + 문제 핵심 풀이 가 한번에 이루어지고 있죠. 지금은 문제가 되지 않지만 더 긴 코드를 작성할 때, 함수 이름만 보고 solution 함수를 계속 호출한다면 어떻게 될까요? 이 부분 꼭 함수에 문제 핵심 풀이만 담기도록 수정 부탁드려요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

꼼꼼한 리뷰 정말 감사합니다!
ans[i] = solution(v, n); 1946번 여기서 궁금한 점이 있는데요, 말해주신대로 수정을 하다가 ans[t]를 배열로 했을 때는 오류가 나는데 벡터로 했을 때는 오류가 아니더라고요. 이유가 뭔지 잘 모르겠어요!

Copy link

@bsa0322 bsa0322 Sep 9, 2021

Choose a reason for hiding this comment

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

엇 말씀해주신대로 벡터만 배열 or 배열을 벡터로 바꾼거라면 오류가 나거나 달라질 수 없습니다! IDE에 옮겨서 확인도 해봤는데 저는 괜찮았어요..! 혹시 ans[t]를 배열 사용 시, 다른 코드 부분이 달랐던 건 아닐까요..? 그 때의 전체 코드를 주시면 확인해드릴께욥!!

++ 지금 리뷰하면서 발견한건데요..! 혹시 solution 함수로 넘기는 부분에서의 매개변수 타입을 일치시키지 않은 거려나요....!? 지금으로선 이 가능성이 가장 클 것 같아요!

++ 엇 그런데 또 ans만 배열로 바꾸시고, v는 계속 벡터로 사용하셨다면 매개변수 부분도 문제가 되진 않았을 것 같은데요...우선 말씀하신대로라면 오류가 안나야 하는게 맞습니다!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

죄송합니다 다시 해보니까 오류가 없습니다ㅜㅜ

Copy link

Choose a reason for hiding this comment

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

아핫 다행이에요!!!!


sort(v.begin(), v.end());

int count = n;
int temp = v[0].second;
for(int i=1; i<n; i++){
if(v[i].second > temp)
count--;
Copy link

Choose a reason for hiding this comment

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

오! 불합자를 찾아내는 풀이를 생각해주셨군요. 지금도 좋지만 합격자를 찾는 풀이로 하면 if else로 나누지 않고 아래 코드를 바로 합칠 수 있어요!

else
temp = v[i].second;
}

return count;

}

int main(){
int t;
cin >> t;

int answer[t];

for(int i=0; i<t; i++){
int n;
cin >> n;
answer[i] = solution(n);
}

for(int i=0; i<t; i++){
cout << answer[i] << '\n';
}
}