C, C++/๐Ÿฅˆ BOJ

(C++) โ˜…Sorting Intermediate I - 5 Solvedโ˜…

metamong 2024. 11. 14.

โ˜… 1427 ์†ŒํŠธ์ธ์‚ฌ์ด๋“œ โ˜…

//1427
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string N;
    cin >> N;

    sort(N.begin(), N.end(), greater<char>());

    cout << N;
    
	return 0;
}

 

๐Ÿš€ ๋‚ด๋ฆผ์ฐจ์ˆœ์€ sort()์˜ ์„ธ๋ฒˆ์งธ ์ธ์ž์— greater<>๋ฅผ ๋„ฃ๋Š”๋‹ค. ์ด ๋•Œ, ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค๋ฉด greater<char>. ๋งŒ์•ฝ์— intํ˜• ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ„ array๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค๋ฉด greater<int>๋ฅผ sort()์˜ ์„ธ๋ฒˆ์งธ ์ธ์ž๋กœ ๋„ฃ๋Š”๋‹ค.


โ˜… 11650 ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ โ˜…

//11650
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

bool compare(pair<int,int> a, pair<int,int> b){
    if(a.first == b.first){
        return a.second < b.second;
    }
    else{
        return a.first < b.first;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N, x, y;
    cin >> N;
    int tmp = N;

    vector<pair<int,int>> v;

    while(tmp--){
        cin >> x >> y;
        v.push_back({x, y});
    }

    sort(v.begin(),v.end(),compare);
    for(int i = 0; i < N; i++){
        cout << v[i].first << ' ' << v[i].second << '\n';
    }

	return 0;
}

 

๐Ÿš€ ๋‘ int๋ฅผ ๋™์‹œ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” vector<pair<int,int>> v ์ƒ์„ฑ. v.push_back({x.y})๋กœ ๋‘ ์ •๋ณด ๋™์‹œ์— ๋„ฃ์€ ๋’ค, sorting.

 

๐Ÿš€ sorting ์ง„ํ–‰ ์‹œ ์„ธ๋ฒˆ์งธ ์ธ์ž bool ํ•จ์ˆ˜ compare ๋ณ„๋„ ํ™œ์šฉ. compare() ํ•จ์ˆ˜์—์„œ ๋‘ a์™€ b๊ฐ€ ๊ฐ™๋‹ค๋ฉด a.second < b.second๋กœ second ์˜ค๋ฆ„์ฐจ์ˆœ / ์„œ๋กœ ๋‹ค๋ฅด๋‹ค๋ฉด a.first < b.first๋กœ first ์˜ค๋ฆ„์ฐจ์ˆœ. ์ฆ‰, compare๋กœ ์ง์ ‘ sorting ์‚ฌ์šฉ์ž ์ง€์ • ๊ฐ€๋Šฅ


โ˜… 11651 ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ 2 โ˜…

//11651
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

bool compare(pair<int,int> a, pair<int,int> b){
    if (a.second == b.second){
        return a.first < b.first;
    }
    else{
        return a.second < b.second;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N, tmp, x, y;
    vector<pair<int,int>> v;
    cin >> N;
    tmp = N;

    while(tmp--){
        cin >> x >> y;
        v.push_back({x,y});
    }

    sort(v.begin(),v.end(),compare);
    for(int a = 0; a < v.size(); a++){
        cout << v[a].first << ' ' << v[a].second << '\n';
    }

	return 0;
}

 

๐Ÿš€ y์ขŒํ‘œ ๋จผ์ €์ด๋ฏ€๋กœ bool compare()์˜ ๋‚ด์šฉ๋งŒ ๋ฐ”๊พธ๋ฉด ๋


โ˜… 1181 ๋‹จ์–ด ์ •๋ ฌ โ˜…

//1181
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

bool compare(string x, string y){
    if (x.length() == y.length()){
        return x < y;
    }
    else{
        return x.length() < y.length();
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N,tmp,i=0;
    string str[20000];
    cin >> N;
    tmp = N;
    
    while(tmp--){
        cin >> str[i];
        i+=1;
    }

    sort(str,str+N,compare);

    for(int i = 0; i < N; i++){
        if(str[i] == str[i-1]){
            continue;
        }
        cout << str[i] << '\n';
    }

	return 0;
}

 

๐Ÿš€ compare() ํ•จ์ˆ˜๋กœ length ๋™์ผ ์‹œ x<y๋กœ ์‚ฌ์ „ ์ˆœ ์ •๋ ฌ. ๋™์ผํ•˜์ง€ ์•Š๋‹ค๋ฉด ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒƒ๋ถ€ํ„ฐ ์ •๋ ฌ์ด๋ฏ€๋กœ x.length() < y.length() ์‚ฌ์ „ ์„ค์ •

 

๐Ÿš€ string ๋ฌธ์ž์—ด ๋ฐฐ์—ด ๋ฏธ๋ฆฌ ์„ ์–ธํ•œ ๋’ค, sort(str, str + N, compare)๋กœ string ๋ฐฐ์—ด ๋งจ ์™ผ์ชฝ๋ถ€ํ„ฐ N๊ฐœ์˜ ๋‹จ์–ด๊นŒ์ง€ compare

 

๐Ÿš€ ์ด ๋•Œ ์ค‘๋ณต์€ ํ•œ ๋ฒˆ๋งŒ ์ถœ๋ ฅ์ด๋ฏ€๋กœ (str[i] == str[i-1]) ์ผ๋•Œ continue


โ˜… 10814 ๋‚˜์ด์ˆœ ์ •๋ ฌ โ˜…

//10814
#include <iostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

bool compare(pair<int, string> a, pair<int, string> b){
    return a.first < b.first;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N,tmp,age;
    string name;
    vector<pair<int,string>> v;

    cin >> N;
    tmp = N;

    while(tmp--){
        cin >> age >> name;
        v.push_back({age, name});
    }

    stable_sort(v.begin(), v.end(), compare);

    for(int x = 0; x < N; x++){
        cout << v[x].first << ' ' << v[x].second << '\n';
    }


	return 0;
}

 

๐Ÿš€ push_backํ•œ ์ˆœ์„œ๋Œ€๋กœ ๊ทธ ์ˆœ์„œ ๊ทธ๋Œ€๋กœ ์•ˆ์ •์ ์ด๊ฒŒ ์ •๋ ฌ์„ ํ•˜๊ธฐ ์œ„ํ•ด sort()๊ฐ€ ์•„๋‹Œ stable_sort()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.(๋‚˜์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ๋จผ์ € ๊ฐ€์ž…ํ•œ ์‚ฌ๋žŒ์ด ์•ž์— ์˜ค๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌ์ด๋ผ ํ•˜์˜€์œผ๋ฏ€๋กœ ์ˆœ์„œ ์œ ์ง€๋ฅผ ์œ„ํ•ด stable_sort())


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€