Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
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';
}
}
36 changes: 36 additions & 0 deletions 9월 7일 - 정렬/1316.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
using namespace std;

bool checker(string s){
bool check [26] = {};
int len = s.length();

for(int j=0; j<len; j++){
char ch = s[j];
if (check[ch - 'a'])
return false;
check[ch-'a'] = true;
while(s[j+1] == ch) {
Copy link

Choose a reason for hiding this comment

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

오오 함수화와 O(n) 풀이로 너무 잘 해주셨어요!!! 👍 지금처럼 while문으로 한 번에 같은문자를 처리하는 것도 좋지만, 겉에 for문에서 인덱스 증가가 이루어지고 있으므로 이 점을 활용하여 여기서 if문 + continue 로 처리를 하는 것도 좋아요~!

j++;
}
if (j == len - 1)
return true;
}

}

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

for(int i=0; i<n; i++) {
string s;
cin >> s;
if(checker(s))
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;
}
42 changes: 42 additions & 0 deletions 9월 7일 - 정렬/1431.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int sum(string s){
int sum = 0;

for (int j = 0; j < s.length(); j++) {
if (s[j] >= '0' && s[j] <= '9')
sum += s[j]-'0';
}
return sum;
}

bool cmp(const string &i1, const string &i2){
if(i1.length() != i2.length())
return i1.length() < i2.length();
if(sum(i1) != sum(i2))
Copy link

Choose a reason for hiding this comment

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

👍👍👍

return sum(i1) < sum(i2);
return i1 < i2;
}

int main() {
int n;
cin >> n;
string num[n];

//입력
for (int i = 0; i < n; i++) {
cin >> num[i];
}

//정렬
sort(num, num+n, cmp);

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

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

using namespace std;

int solution(vector<pair<int, int>> v, int n) {

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

return count;

}

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

vector<int> ans(t);

//입력, 정렬
for(int i=0; i<t; i++){
int n;
cin >> n;
vector<pair<int, int>> v(n);
for(int j=0; j<n; j++){
cin >> v[j].first >> v[j].second;
}

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

ans[i] = solution(v, n);
Copy link

Choose a reason for hiding this comment

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

👍👍👍

또한 지금 테스트케이스 수만큼의 ans를 계속 저장해주셨는데, 이것도 좋지만 변수로 선언해서 바로바로 ans를 출력해도 괜찮아요!!!! 이렇게하면 마지막 출력을 위한 for문도 사용하지 않아도 되겠죠? 😊

}

//출력
for(int i=0; i<t; i++){
cout << ans[i] << '\n';
}
}